我正在尝试编写一个Teamcenter ITK程序,它将作为从主线程调用的不同线程运行。主线程是从UI上的操作调用的。由于子线程需要很长时间才能完成,如果我不创建子线程并将代码放入主线程中,UI将冻结长达10分钟,这是不可接受的。
主线程和子线程都需要共享由主线程完成的身份验证,因为我使用的是SSO。它们还需要连接到数据库。最后,主线程不应等待子线程完成,否则将无法实现使用子线程的全部目的。
调用子线程的代码如下:
handle = (HANDLE) _beginthread (submitToPublishTibcoWf, 0, &input); // create thread
do
{
sprintf(message, "Waiting %d time for 1000 milliseconds since threadReady is %d\n", i++, threadReady);
log_msg(message);
WaitForSingleObject(handle, 1000);
}
while (!threadReady);
sprintf(message, "Wait for thread to be ready over after %d tries since threadReady is %d\n", i, threadReady);
log_msg(message);
log_msg("Main thread about to exit now");每当我要执行子线程中需要运行8分钟的代码时,我都会设置threadReady = 1全局变量。
问题是,在主线程退出后,子线程的行为异常,我得到了这个错误:
Fri May 25 11:34:46 2012 : Main thread about to exit now
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.大多数子线程都会执行,但有时会在最后崩溃。
发布于 2012-05-26 13:34:21
为了防止退出子线程,我们可以使用分离来使子进程独立,并且不期望连接到父进程。因此,我们不应该连接子进程,在此之后,我们必须与主线程分离:
pthread_create(th, attr, what);
pthread_detach(th);
// and never join另外:
threadReady这样的观察信号特殊事件的详尽监听。相反,使用pthread中的条件变量或gObject.https://stackoverflow.com/questions/10759449
复制相似问题