注释“pthread_exit(NULL)”时同步失败的原因在该计划中。 [英] why synchronization failed when commenting "pthread_exit(NULL)" in the program.

查看:111
本文介绍了注释“pthread_exit(NULL)”时同步失败的原因在该计划中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从pthread_exit(NULL)删除注释时代码工作正常。

我没有得到它,为什么.. ??



The code working fine when removing comment from "pthread_exit(NULL)".
I am not getting it, why..??

#include< stdio.h>
#include< pthread.h >
#include< stdlib.h >
#include< string.h >
#define N 200
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
char *n;
int i=1,comp=1;
void * read1()
{
    while(comp)
    {
        pthread_mutex_lock(&mutex);
        n=(char *)malloc(N*sizeof(char));
        printf("\nEnter a string:\t");
        gets(n);
        if((i=strcmp("end",n))==0)
            comp=0;
        pthread_mutex_unlock(&mutex);
        usleep(10);
    }}
void * write1()
{
    while(comp)
    {
        usleep(50);
        pthread_mutex_lock(&mutex);
        if(i!=0)
            printf("\nThe string entered is : %s\n",n);
        pthread_mutex_unlock(&mutex);
    }}
int main()
{
    pthread_t tr, tw;
    int res;
    pthread_mutexattr_t attr;
    pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
    res=pthread_mutex_init(&mutex,&attr);
    pthread_create(&tr,NULL,read1,NULL);
    pthread_create(&tw,NULL,write1,NULL);
    //pthread_exit(NULL);
    return 0;
}

推荐答案

main()函数被调用/由运行时库执行。运行时lib在您启动程序时执行以下操作:

1.运行时库的初始化

2.程序中全局变量的初始化

3.调用 main()您的程序

4.销毁程序中的全局变量

5 。运行时库的初始化

6.进程终止。



如果退出主线程而没有从 main()函数然后执行步骤4,5和6,并且当所有线程退出时程序终止。 (注意,杀死/终止主线程与终止/终止进程不同!(...但这可能是特定于平台的。))如果你不调用 pthread_exit(NULL)<来自主线程的/ code>将执行步骤4,5和6,无论工作线程的进度如何,步骤6都会终止您的进程。正确的解决方案不是用 pthread_exit(NULL)来杀死主线程,而是在从main返回之前加入两个工作线程(等待终止)。



(附注:第4步,第5步和第6步不仅在你从main返回时执行,而且如果你调用运行时库的exit()函数也会执行。)
The main() function is called/executed by the runtime library. The runtime lib does the following when you start your program:
1. Initialization of the runtime library
2. Initialization of global variables in your program
3. calling main() of your program
4. Destroying the global variables in your program
5. Deinitialization of the runtime library
6. Process termination.

If you exit your main thread without returning from your main() function then the steps 4, 5, and 6 will never be executed and your program terminates when all threads exit. (Note that killing/terminating the main thread is not the same as terminating/killing the process! (...but this can be platform specific.)) If you don't call pthread_exit(NULL) from your main thread then steps 4, 5, and 6 will be executed and step 6 kills your process no matter what is the progress of your worker threads. The correct solution is not to kill your main thread with pthread_exit(NULL) but joining both worker threads (to wait for their termination) before returning from main.

(Side note: step 4, 5 and 6 get executed not only if you return from main but also if you call the exit() function of the runtime library.)


这篇关于注释“pthread_exit(NULL)”时同步失败的原因在该计划中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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