Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.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_exit?_C_Multithreading_Unix_Pthreads - Fatal编程技术网

C 为什么要使用pthread_exit?

C 为什么要使用pthread_exit?,c,multithreading,unix,pthreads,C,Multithreading,Unix,Pthreads,我正试图通过以下示例代码了解pthread\u exit的用法: void* PrintVar(void* arg) { int * a = (int *) arg; // we can access memory of a!!! printf( "%d\n", *a); } int main(int argc, char*argv[]) { int a, rc; a = 10; pthread_t thr; pthread_crea

我正试图通过以下示例代码了解
pthread\u exit
的用法:

void* PrintVar(void* arg)
 { 
   int * a = (int *) arg; // we can access memory of a!!!
    printf( "%d\n", *a); 
 } 

int main(int argc, char*argv[]) 
 { 
   int a, rc;
    a = 10; 
   pthread_t thr; 
   pthread_create( &thr, NULL, PrintVar, &a ); 

  //why do I need it here?//
  pthread_exit(&rc); /* process continues until last  
                                threads termintates */
有两件事我不太确定:

  • 当我们使用pthread_create-我传递'a'参数的地址时, 但是这个参数是否保存在PrintVar函数的“arg”下? 例如,如果我正在使用:
    PrintVar(void*blabla)
    ,并且希望从主函数传递两个参数:
    inta=10,intb=20
    。。我该怎么做

  • 为什么需要pthread_退出?这意味着-等待进程结束-但如果我不使用该行,会出现什么情况

  • 非常感谢

  • 当我们使用pthread_create-时,我正在传递'a'参数的地址,但是这个参数是否在PrintVar函数的“arg”下“保存”
  • “原始”
    a
    (在
    main
    中定义的一个)没有被复制,您只是传递一个指向它的指针

    例如,如果我使用:PrintVar(void*blabla),并希望从主函数传递2个参数:int a=10,int b=20。。我该怎么做

    将这两个值放入
    结构中
    ,并将指向该结构的指针作为参数传递给
    pthread\u create
    (因此,PrintVar将接收这样的指针并能够检索这两个值)

    我的第二个问题是为什么需要pthread_退出?这意味着-等待进程结束-但如果我不使用该行,会出现什么情况

    pthread\u exit
    如果其他线程仍在运行,则终止当前线程而不终止进程;相反,从
    main
    返回相当于调用
    exit
    ,就标准而言,它应该“终止程序”(从而隐式终止所有线程)

    现在,作为C标准的线程不可知论者(直到C11)和对各种Unix中线程的支持,一个相对较新的补充,取决于libc/kernel/任何版本
    exit
    可能只杀死当前线程或所有线程

    尽管如此,在当前版本的libc中,
    exit
    (从而从
    main
    返回)应该终止进程(以及它的所有线程),实际上在Linux上使用syscall
    exit_组


    请注意,a适用于Windows CRT。

    分离属性仅确定线程终止时系统的行为;事实并非如此 如果进程使用exit(3)终止(或者如果
    主线程返回)。

    参考第二个问题:您是否尝试过将发生什么?提示:添加一个
    sleep(1)
    PrintVar()
    的开头,并注释掉对
    pthread\u exit()
    的调用。这些是多线程的基本问题,也是非常智能的问题。也许一本好书会有所帮助more@alk我在没有睡眠的情况下使用了它,它的工作原理与没有线程的情况下是一样的。这就是为什么我感到困惑:(我会试试你的建议!谢谢()有点类似的问题所以如果我使用exit())它将终止所有线程,但如果我使用pthread_exit,它将等待哪个线程完成?正在运行的线程?这意味着操作系统不能在“中间”停止线程?@user1386966:如果您使用
    pthread\u exit
    它将只终止当前线程,其余线程将继续运行;当最后一个线程终止时,进程将自动终止。使用
    exit
    您将残酷地终止所有线程。请注意,对于“正常”线程,从其入口点返回相当于调用
    pthread_exit
    ;问题只在于主线程,因为从
    main
    返回相当于调用
    exit
    +1我要补充的是,传递自动存储(堆栈)的地址是一种相当糟糕的做法线程启动例程的变量——当新线程醒来检查其参数时,该堆栈可能会消失。@pilcrow所说的,这不仅是不好的实践,而且非常危险。传递给线程函数的指针应该动态分配(并且它们的所有权转移到线程),或指向全局数据。@pilcrow如果有一个
    pthread\u join(thr)
    而不是
    pthread\u exit
    ,那么当线程检查堆栈时,是否可以安全地假设该值会出现在堆栈上?[正在检查代码,对此有疑问]欢迎来到SO!检查问题并检查您的答案。这与其说是答案,不如说是评论。请编辑或删除,因为某些OP可能会否决它。