简体   繁体   English

C ++中的Pthread取消

[英]Pthread Cancellation in c++

The following (pseudo-) code induces some behaviour which I dont understand. 以下(伪)代码引发了一些我不理解的行为。 I have 2 threads running parallel. 我有两个并行运行的线程。 Both do the same (complex) calculation but I dont know which will finish first. 两者都进行相同(复杂)的计算,但我不知道哪个会先完成。 Repeatedly run, there are cases where the first is faster and cases where the second is faster. 反复运行,有些情况下第一个更快,第二个更快的情况。 This is okay and works as intended. 这没关系,按预期工作。 Then, the first successfull thread should terminate the other thread and both should fork together. 然后,第一个成功的线程应该终止另一个线程,两者都应该一起分叉。 However if the first solver finishs, everything works out but if the second finishes first, the "join command" does not recognize that the first solver is terminated (so the join waits forever, the programm does not continue). 但是,如果第一个求解器完成,则一切正常,但如果第二个求解器首先完成,则“join命令”无法识别第一个求解器终止(因此连接等待永远,程序不会继续)。 Any ideas what I did wrong or what I could do different? 我做错了什么或者我能做些什么不同的任何想法?

void* thread_function(...)
{
   do_some_complex_task();
   if (successfull && first_finsihed)
   {
        pthread_chancel(other_thread);
   }
}

int main()
{
    std::vector<pthread_t*> store;
    store.resize(2);

    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    pthread_create(store[0], NULL, thread_function, ...);
    pthread_create(store[1], NULL, thread_function, ...);

    pthread_join(*store[0], NULL);
    pthread_join(*store[1], NULL);
}

PS. PS。 If the pseudo code is not detailed enough please let me know. 如果伪代码不够详细,请告诉我。

Don't use pthread_cancel - use a global state, and check it inside the "main loop" of each thread . 不要使用pthread_cancel - 使用全局状态,并在每个线程的“主循环”中检查它。 Set it "on" just before the exit of the thread's function. 在线程函数退出之前将其设置为“on”。

Based on the pseudo code, one problem may be that the threads have deferred cancellation (the default value) instead of asynchronous cancellation. 基于伪代码,一个问题可能是线程具有延迟取消(默认值)而不是异步取消。 If the canceled thread never reaches a cancellation point , then pthread_join would block. 如果取消的线程永远不会到达取消点 ,则pthread_join将阻止。 When calling pthread_create , the newly created thread inherits the calling thread's: 调用pthread_create ,新创建的线程继承调用线程:

Try invoking pthread_setcanceltype from within threads you wish to cancel. 尝试从您要取消的线程中调用pthread_setcanceltype You may also want to consider attaching a debugger to the program to identify the current state of the threads. 您可能还需要考虑将调试器附加到程序以识别线程的当前状态。

Ideally, if it is at all possible, consider avoiding pthread_cancel . 理想情况下,如果可能的话,请考虑避免使用pthread_cancel Although the pthread-calls are documented, it can be difficult to obtain and maintain the exact behavior due to all of the minor details. 尽管记录了pthread调用,但由于所有细微的细节,很难获得并保持确切的行为。 It is generally easier to set a flag that indicates that the thread is set to exit, and begin to return from functions when it is set. 通常更容易设置一个标志,指示线程设置为退出,并在设置时从函数开始返回。 In do_some_complex_task , consider breaking the complex task into smaller tasks, and check for cancellation between each smaller task. do_some_complex_task ,考虑将复杂任务分解为较小的任务,并检查每个较小任务之间的取消。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM