Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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
C 如何收集pthread的用户定义退出状态_C_Pthreads - Fatal编程技术网

C 如何收集pthread的用户定义退出状态

C 如何收集pthread的用户定义退出状态,c,pthreads,C,Pthreads,如何使用pthread\u cancel 在程序下面,我试图使用PTHREAD\u cancelled和其他用户指定的值检查状态 对于取消的线程,如果(status==PTHREAD\u cancelled)为true,但对于用户指定的退出值 如果(status==errNum)不是真的,如果handle如果(status=*(int*)errNum)也是真的,但是如果我用同样的方法通过*(int*)PTHREAD\u cancelled来了解PTHREAD\u cancelled的值,这将是一

如何使用
pthread\u cancel


在程序下面,我试图使用
PTHREAD\u cancelled
和其他用户指定的值检查状态

对于取消的线程,如果(status==PTHREAD\u cancelled)为true,但对于用户指定的退出值
如果(status==errNum)
不是真的,如果handle
如果(status=*(int*)errNum)
也是真的,但是如果我用同样的方法通过
*(int*)PTHREAD\u cancelled
来了解
PTHREAD\u cancelled
的值,这将是一个segfault,为什么

顺便问一下,还有一个问题,是否允许从自身取消同一个线程
pthread_cancel(pthread_self())

#包括
#包括
void*thread_func(void*arg);
void*thread_func2(void*arg);
int errNum=3;
pthread_t st_id;
int main()
{
pthread_t_id,t_id2;
无效*状态;
//成功时pthread_create返回零
if(pthread\u create(&t\u id,NULL,thread\u func,NULL)!=0){
printf(“线程创建失败\n”);
返回0;
}
printf(“已成功创建id为%u的线程”,t\u id);
st_id=t_id;
if(pthread_create(&t_id2,NULL,thread_func2,NULL)!=0){
printf(“线程创建失败\n”);
返回0;
}
printf(“已成功创建id为%u的线程”,t\u id2);
if(pthread_join(t_id和状态)==0){
printf(“加入成功”\n);
如果(状态==PTHREAD\u已取消){
printf(“线程%u已成功取消,值=%u\n”,线程id,*(int*)状态);
}否则,如果(状态){
printf(“线程%u已退出,状态为%u\n”,t\u id,状态);
}
}
否则{
printf(“加入失败\n”);
}
if(pthread_join(t_id2,&status)==0){
printf(“tid2的加入成功”\n);
如果(状态==PTHREAD\u已取消){
printf(“线程%u已成功取消,值=%u\n”,t_id2,*(int*)状态);
}否则,如果(状态){
printf(“线程%u已退出,状态为%u\n”,t_id2,状态);
}
}
否则{
printf(“tid2的加入失败\n”);
}
返回0;
}
void*thread_func(void*arg)
{
printf(“内部线程函数:%u\n”,pthread_self());
睡眠(3);
pthread_退出(0);
}
void*thread_func2(void*arg)
{
printf(“内部线程函数2:%u\n”,pthread_self());
pthread_cancel(st_id);
pthread_退出(&errNum);
}

PTHREAD\u cancelled
是已取消线程的退出状态。它不太可能是int的有效地址,因此当您尝试将其视为一个时,会出现segfault。好吧,这是合理的,但是如果我必须对任何用户定义的值
if(status==PTHREAD\u user\u defined\u STATUS1)
,进行同样的检查,那么,我们如何在
pthread\u exit
中传递状态,在
pthread\u join
中收集,被取消的线程无法将退出状态传递给
pthread\u join()
。线程被取消;它无法选择传递给
pthread\u join()
的值。如何定义
errNum
以及在哪里定义?发布的代码不会编译。编译器输出许多警告消息,其中有几条非常严重。编译时,始终启用警告,然后修复这些警告。(对于
gcc
,至少使用:
-Wall-Wextra-Wconversion-pedantic-std=gnu11
)注意:其他编译器使用不同的选项生成相同的内容
#include <stdio.h>
#include <pthread.h>

void *thread_func(void *arg);
void *thread_func2(void *arg);
int errNum = 3;
pthread_t st_id;

int main()
{
    pthread_t t_id, t_id2;
    void *status;

    // on success pthread_create return zero
    if(pthread_create(&t_id,NULL,thread_func,NULL) != 0){
        printf("thread creation failed\n");
        return 0;   
    }
    printf("thread created with id %u successfully\n",t_id);
    st_id = t_id;

    if(pthread_create(&t_id2,NULL,thread_func2,NULL) != 0){
        printf("thread creation failed\n");
        return 0;   
    }
    printf("thread created with id %u successfully\n",t_id2);

    if(pthread_join(t_id,&status) == 0){
        printf("join success \n");
        if(status == PTHREAD_CANCELED){
            printf("Thread %u cancelled successfully,value =%u\n",t_id,*(int *)status);
        }else if(status){
            printf("thread %u exited with status %u\n",t_id,status);
        }
    }
    else {
        printf("join failed \n");
    }

    if(pthread_join(t_id2,&status) == 0){
        printf("join success for tid2\n");
        if(status == PTHREAD_CANCELED){
            printf("Thread %u cancelled successfully, value = %u\n",t_id2,*(int *)status);
        }else if(status){
            printf("thread %u exited with status %u\n",t_id2,status);
        }
    }
    else {
        printf("join failed for tid2\n");
    }

    return 0;
}


void *thread_func(void *arg)
{
    printf("Inside thread_func :%u\n",pthread_self());
    sleep(3);
    pthread_exit(0);

}


void *thread_func2(void *arg)
{
    printf("Inside thread_func2 :%u\n",pthread_self());
    pthread_cancel(st_id);
    pthread_exit(&errNum);
}