【问题标题】:Multithreading in Teamcenter ITKTeamcenter ITK 中的多线程
【发布时间】:2012-06-01 07:59:58
【问题描述】:

我正在尝试编写一个 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.

大部分子线程都会执行,但有时它会在最后崩溃。

【问题讨论】:

  • 有时我在调试时也会收到此错误:tcserver.exe 中 0x580d8390 处的未处理异常:0xC0000005:访问冲突读取位置 0xffffffec。

标签: c multithreading teamcenter teamcenter-itk


【解决方案1】:

为了防止子线程退出,我们可以使用分离来使子进程独立并且不期望加入父进程。 因此我们不应该加入子进程,然后,我们必须从主线程中分离:

pthread_create(th, attr, what);
pthread_detach(th);
// and never join

还有:

  1. 如果你想为你的应用程序增加一些效率,我建议不要像threadReady那样使用穷举监听观察信号的特殊事件。而是使用pthread 中的条件变量或gObject 等其他信号方法。
  2. 您有一些线程之间共享的数据,它可能面临互斥问题和其他问题,例如在多处理或多线程应用程序中可能发生的问题。而是尝试通过使用一些机制(如互斥锁或条件变量的信号量。

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    相关资源
    最近更新 更多