pthread_cancel可以()函数失败终止线程 [英] pthread_cancel() function failed to terminate a thread

查看:432
本文介绍了pthread_cancel可以()函数失败终止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用在pthread_create() pthread_cancel可以()函数来创建多线程程序,但我注意到该 pthread_cancel可以()并没有真正终止它应该线程。

I'm using pthread_create() and pthread_cancel() functions to create a multithreaded program, but I noticed that pthread_cancel() did not really terminate the thread it was supposed to.

void check(void *param){
    header_t *Bag = (header_t *) param;
    pthread_t timer_thread;
    while(true){
        if(Bag->size && TIMER_OFF){
            pthread_create(&timer_thread, NULL, (void *) &timer, (void *) Bag);
            printf("\nCREATE THREAD ID = %d\n", timer_thread);
            // ADD
        }
        else if(!TIMER_OFF && Bag->size >= 2 && Bag->next->time <= CURRENT_APT_TIME && CRRENT_TAG != Bag->next->tag){
            printf("\nOLD THREAD ID = %d TO BE CANCELLED\n", timer_thread);
            pthread_cancel(timer_thread);
            pthread_create(&timer_thread, NULL, (void *) &timer, (void *) Bag);
            printf("\nNEW THREAD ID = %d\n", timer_thread);
            // Replace
        }
        Sleep(1);
    }
}

定时器功能无效定时器(无效*)正是这听起来像,我已经包括几行打印出自身的线程ID。

timer function void timer(void *) is exactly what it sounds like, and I've included couple of lines to print out the thread ID of itself.

在测试,下面是看出:

...

OLD THREAD ID = 6041240 TO BE CANCELLED

NEW THREAD ID = 6046456

...

THREAD ID EXECUTING = 6041240

因此​​,定时器功能没有被调用终止 pthread_cancel可以()

推荐答案

在默认情况下你的线程与的取消类型 PTHREAD_CANCEL_DEFERRED ,这意味着你需要确保你的线程都有一个所谓的取消点。也就是说,一个点分别取消请求进行检查并在反应

By default your thread is created with the cancel type PTHREAD_CANCEL_DEFERRED, which means that you need to make sure that your thread has a so-called cancellation point. That is, a point were cancel requests are checked and reacted upon.

此页面包含那保证是取消点的功能列表。例子是一些睡眠()和pthread函数的。您还可以使用 pthread_testcancel()如果你只需要一个函数纯粹测试线程是否已经取消了,仅此而已。

This page contains a list of functions that are guaranteed to be cancellation points. Examples are some of the sleep() and pthread functions. You can also use pthread_testcancel() if you just need a function to purely test whether the thread has been canceled, and nothing else.

另一种选择是设置你的线程canceltype为 PTHREAD_CANCEL_ASYNCHRONOUS ,使用 pthread_setcanceltype(),这将使你的线程取消,即使没有取消点。然而,系统仍然可以自由选择何时线程实际上应该被取消,你会需要采取非常谨慎,以确保取消当系统不处于不一致的状态(通常是指避免任何的系统调用和类似 - 参见href=\"http://www.kernel.org/doc/man-pages/online/pages/man7/pthreads.7.html\" rel=\"nofollow\"> - 这是短)

Another option is to set your threads canceltype to be PTHREAD_CANCEL_ASYNCHRONOUS, by using pthread_setcanceltype(), which will make your thread cancelable even without cancellation points. However, the system is still free to choose when the thread should actually be canceled, and you'll need to take great care to make sure that the system isn't left in an inconsistent state when cancelled (typically means avoiding any system calls and similar - see the list of Async-cancel-safe functions - it's short!).

在一般情况下,异步取消应该只用于或多或少纯粹的处理线程,线程而执行系统调用和类似的是更好地推迟和取消的取消点小心放置实现的。

In general, asynchronous cancellation should only be used for more or less "pure" processing threads, whereas threads performing system calls and similar are better implemented with deferred cancellation and careful placement of cancellation points.

另外,只要你是不是卸下你的线程(通过创建它通过它的属性分离,或致电 pthread_detach()它在创建后),您将需要调用在pthread_join()计时器线程上,以确保所有的资源都取消后清理。否则,你可能在一个国家最终没有任何空闲的线程资源,在那里你不能创建任何新的线程。

Also, as long as you are not detaching your thread (by either creating it detached through its attribute, or by calling pthread_detach() after it is created), you will need to call pthread_join() on the timer thread to make sure that all resources are cleaned up after canceling it. Otherwise you might end up in a state without any spare threading resources, where you cannot create any new threads.

这篇关于pthread_cancel可以()函数失败终止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆