我在我的应用程序中使用NSPR 线程。我正在创建 50 个线程。每个线程都会打开 SQLite 数据库并执行查询。
当两个线程同时执行查询时,将调用函数 BusyHandler(在下面的程序中)。我们如何在 BusyHandler 函数中获取 threadid?
示例程序:
#define THREADCOUNT 50
static int BusyHandler(void * pArg, int pTries);
void CreateThreads()
{
int i = 0;
PRThread *thread[THREADCOUNT];
for(i = 0; i<THREADCOUNT;i++)
{
waitobj[i] = new WaitObj();
thread[i] = PR_CreateThread(PR_USER_THREAD, StartThread, (void *)i, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
}
Sleep(20000);
for(i = 0; i<THREADCOUNT;)
{
waitobj[i]->Signal();
PR_JoinThread(thread[i]);
i = i+1;
}
}
void StartThread(void *Arg)
{
char *errmsg;
const char *err;
const char *unused;
int ret;
int index = (int)Arg;
sqlite3 *sqlite;
const char *dir = "test.sqlite";
ret = sqlite3_open(dir, &sqlite);
sqlite3_busy_handler(sqlite, BusyHandler, sqlite);
char query[100];
sqlite3_stmt *stmt;
strcpy(query, "insert into table1 values(1,2,3,4,5)");
ret = ExecuteQuery(sqlite, query);
if(ret != SQLITE_OK && ret != SQLITE_DONE )
{
return;
}
Sleep(10000);
}
int BusyHandler(void * pSQLite, int pTries)
{
sqlite3 * sqlite = (sqlite3 *)pSQLite;
Sleep(1000);
if(pTries >= 10) // now return BUSY
return 0;
// check whether lock is removed!
return 1;
}