Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
macos-macos';s pthread pthread_CANCEL_异步不工作_C_Multithreading_Macos_Pthreads - Fatal编程技术网

macos-macos';s pthread pthread_CANCEL_异步不工作

macos-macos';s pthread pthread_CANCEL_异步不工作,c,multithreading,macos,pthreads,C,Multithreading,Macos,Pthreads,我正在使用以下代码: #include <unistd.h> #include <pthread.h> #include <stdio.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void clean(void *arg) { printf("thread clean: %ld\n", (unsigned long)arg); } void* thread_template(void

我正在使用以下代码:

#include <unistd.h>
#include <pthread.h>
#include <stdio.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void
clean(void *arg)
{
    printf("thread clean: %ld\n", (unsigned long)arg);
}

void*
thread_template(void *arg)
{
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    pthread_cleanup_push(clean, (void*)pthread_self());
    printf("thread: %ld\n", (unsigned long)pthread_self());
    while ( 1 ) {}
    pthread_cleanup_pop(0);
    pthread_exit((void*)0);
}

int
main(int argc, char const *argv[]) {

    pthread_t tid;
    pthread_create(&tid, NULL, thread_template, NULL);

// waiting for 2 seconds
    sleep(2);

    pthread_cancel(tid);
    pthread_join(tid, NULL);
}
#包括
#包括
#包括
pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项;
无效的
清洁(无效*参数)
{
printf(“线程清理:%ld\n”,(无符号长)arg);
}
空虚*
线程模板(void*arg)
{
pthread_setcancelstate(pthread_CANCEL_ENABLE,NULL);
pthread_setcanceltype(pthread_CANCEL_异步,NULL);
pthread_cleanup_push(clean,(void*)pthread_self());
printf(“线程:%ld\n”,(无符号长)pthread_self();
而(1){}
pthread_cleanup_pop(0);
pthread_exit((void*)0);
}
int
main(int argc,char const*argv[]){
pthread_t tid;
pthread_create(&tid,NULL,thread_模板,NULL);
//等待2秒钟
睡眠(2);
pthread_cancel(tid);
pthread_join(tid,NULL);
}
它在FreeBSD 11和Ubuntu 16上都能正常工作, 输出如下:

线程:1994462320
螺纹清洁:1994462320

但在macOS上,pthread_cancel()似乎不影响线程,主线程在pthread_join()处阻塞,clean函数从不执行(只输出第一行)


那么,macOS的pthread怎么了?或者我的代码有什么问题?

达尔文的pthread_cancel似乎存在一些长期存在的问题,有人说如果线程进行正确的系统调用时取消请求挂起,它将起作用。但是,通常不建议取消线程。它的可移植性不强(正如您所发现的,例如,Linux劫持了一个信号来实现它),而且它不是构建程序的好方法。最好使用其他东西(条件变量、信号量,甚至ZeroMQ[我最喜欢的])向线程发送一些东西,以使其执行路径导致干净的线程终止。@bazza我不知道如何在没有pthread_cancel()的情况下任意停止线程(线程的任何点).@Kevin避免在必要时设计场景。在某个东西中间停下一个线程是危险的——它可能在一个共享资源的操作的中间,因此中断它会使其他线程死锁。