使用了pthread_exit和pthread_join函数。了pthread_exit不会终止呼叫功能 [英] using pthread_exit and pthread_join. pthread_exit doesn't terminate the calling function

查看:688
本文介绍了使用了pthread_exit和pthread_join函数。了pthread_exit不会终止呼叫功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC 4.6.0 C89

我只用了pthread_exit和pthread_join函数进行试验。

我用了pthread_exit注意到它,它没有显示打印信息主返回之前的唯一的事。然而,在pthread_join的确这么做了。

我本来以为应该已经显示的print语句。如果不是这是否意味着主力并没有在使用了pthread_exit正确端接?

非常感谢您的任何建议,

我的源$ C ​​$ C片段由source.c文件:

 无效* process_events(无效)
{
    app_running = TRUE;
    INT计数器= 0;    而(app_running){
#定义TIMEOUT 3000000
        的printf(睡..... \\ n);
        usleep(超时);        如果(计数器++ == 2){
            app_running = FALSE;
        }
    }    的printf(说完处理事件的\\ n);    返回NULL;
}

来源$ C ​​$ C片段main.c文件:

  INT主要(无效)
{
    的pthread_t th_id = 0;
    INT th_rc = 0;    th_rc =在pthread_create(安培; th_id,NULL,(void *的)process_events,NULL);
    如果(th_rc == -1){
        fprintf中(标准错误,无法创建线程[%S] \\ n字符串错误(错误));
        返回-1;
    }    / *
     *测试用了pthread_exit和pthread_join函数
     * /    / *了pthread_exit(NULL); * /    如果(在pthread_join(th_id,NULL)== -1){
        fprintf中(标准错误,无法加入线程[%S]字符串错误(错误));
        返回-1;
    }    的printf(程序终止\\ n);    返回0;
}


解决方案

你看到的预计。 了pthread_exit 绝不会的回报。它停止了立即调用它的线程(,然后运行清理处理程序如果有的话,以及随后可能线程特定数据析构函数)。

中的任何了pthread_exit 将永远运行。

gcc 4.6.0 c89

I am just experimenting with using pthread_exit and pthread_join.

The only thing I notice with the pthread_exit it that it didn't display the print message before main returned. However, pthread_join did exactly that.

I would have thought the print statement should have been displayed. If not does that mean that main has not terminated correctly in using the pthread_exit?

Many thanks for any suggestions,

My source code snippet source.c file:

void* process_events(void)
{
    app_running = TRUE;
    int counter = 0;

    while(app_running) {
#define TIMEOUT 3000000
        printf("Sleeping.....\n");
        usleep(TIMEOUT);

        if(counter++ == 2) {
            app_running = FALSE;
        }
    }

    printf("Finished process events\n");

    return NULL;
}

Source code snippet main.c file:

int main(void)
{
    pthread_t th_id = 0;
    int th_rc = 0;

    th_rc = pthread_create(&th_id, NULL, (void*)process_events, NULL);
    if(th_rc == -1) {
        fprintf(stderr, "Cannot create thread [ %s ]\n", strerror(errno));
        return -1;
    }

    /*
     * Test with pthread_exit and pthread_join
     */

    /* pthread_exit(NULL); */

    if(pthread_join(th_id, NULL) == -1) {
        fprintf(stderr, "Failed to join thread [ %s ]", strerror(errno));
        return -1;
    }

    printf("Program Terminated\n");

    return 0;
}

解决方案

What you're seeing is expected. pthread_exit never returns. It stops the thread that calls it immediately (and then runs the clean-up handlers if any, and then potentially thread-specific data destructors).

Nothing in main after pthread_exit will ever run.

这篇关于使用了pthread_exit和pthread_join函数。了pthread_exit不会终止呼叫功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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