与参数0 pthread_cleanup_pop? [英] pthread_cleanup_pop with argument 0?

查看:139
本文介绍了与参数0 pthread_cleanup_pop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在APUE书2E

我学习线程

我想pthread_cleanup_pop功能是通过pthread_cleanup_push设置推()函数将被执行与否。因此,如果参数为0,它不执行和不为零,执行。

不过,我看了看code图11.5 APUE。这是...

 的#includeapue.h
#包括LT&;&pthreads.h中GT;空虚
清理(​​无效* ARG)
{
    的printf(清理:%S \\ n,(字符*)ARG);
}无效*
thr_fn1(无效* ARG)
{
    的printf(线程1开始\\ n);
    pthread_cleanup_push(清理,线程1先处理程序);
    pthread_cleanup_push(清理,线程1第二处理程序,);
    的printf(线程1推完成\\ n);
    如果(ARG)
        收益率((无效*)1);
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    收益率((无效*)1);
}无效*
thr_fn2(无效* ARG)
{
    的printf(线程2开始\\ n);
    pthread_cleanup_push(清理,线程2先处理程序);
    pthread_cleanup_push(清理线程2第二处理程序,);
    的printf(线程2推完成\\ n);
    如果(ARG)
        了pthread_exit((void *的),2);
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    了pthread_exit((void *的),2);
}INT
主要(无效)
{
    INT犯错;
    的pthread_t TID1,TID2;
    无效* TRET;    ERR =在pthread_create(安培; TID1,NULL,thr_fn1,(无效*)1);
    如果(ERR!= 0)
        err_quit(不能创建线程1:%S \\ n字符串错误(ERR));
    ERR =在pthread_create(安培; TID2,NULL,thr_fn2,(无效*)1);
    如果(ERR!= 0)
        err_quit(不能创建线程2:%S \\ n字符串错误(ERR));
    ERR =在pthread_join(TID1,&安培; TRET);
    如果(ERR!= 0)
        err_quit(不能用螺纹联接1%S \\ n字符串错误(ERR));
    的printf(线程1退出code%d个\\ N(INT)TRET);
    ERR =在pthread_join(TID2,&安培; TRET);
    如果(ERR!= 0)
        err_quit(不能用线加入2%S \\ n字符串错误(ERR));
    的printf(线程2退出code%d个\\ N(INT)TRET);
    出口(0);
}

在该程序中,虽然流行的功能具有零作为参数,则推功能excuted(线程2先处理程序,线程2第二处理程序,,由清理功能打印)

为什么流行音乐作品,即使把0参数?

我才拿到pthread_cleanup_pop错了??


解决方案

  

我才拿到pthread_cleanup_pop错了?


没有,你没有。

您code执行清除处理程序的原因,那是你永远的控制达到 pthread_cleanup_pop(0)。相反,你总是执行在 thr_fn1 收益了pthread_exit thr_fn2

尝试建立与 pthread_create的线程(安培; TID1,NULL,thr_fn1,(无效*)0); 而不是 1 。当我这样做,我得到(预期):

 螺纹1日开始
线程1完全推
线程2开始
线程2完全推
线程1退出code 1
线程2退出code 2

I am studying thread in APUE book 2e

I thought pthread_cleanup_pop functions is for setting the pushed function by pthread_cleanup_push() to be executed or not. So if the argument is zero, it's not executed and non-zero, executed.

But I looked at the code Figure 11.5 in APUE. That is...

#include "apue.h"
#include <pthread.h>

void
cleanup(void *arg)
{
    printf("cleanup: %s\n", (char *)arg);
}

void *
thr_fn1(void *arg)
{
    printf("thread 1 start\n");
    pthread_cleanup_push(cleanup, "thread 1 first handler");
    pthread_cleanup_push(cleanup, "thread 1 second handler");
    printf("thread 1 push complete\n");
    if (arg)
        return((void *)1);
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    return((void *)1);
}

void *
thr_fn2(void *arg)
{
    printf("thread 2 start\n");
    pthread_cleanup_push(cleanup, "thread 2 first handler");
    pthread_cleanup_push(cleanup, "thread 2 second handler");
    printf("thread 2 push complete\n");
    if (arg)
        pthread_exit((void *)2);
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    pthread_exit((void *)2);
}

int
main(void)
{
    int         err;
    pthread_t   tid1, tid2;
    void        *tret;

    err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);
    if (err != 0)
        err_quit("can't create thread 1: %s\n", strerror(err));
    err = pthread_create(&tid2, NULL, thr_fn2, (void *)1);
    if (err != 0)
        err_quit("can't create thread 2: %s\n", strerror(err));
    err = pthread_join(tid1, &tret);
    if (err != 0)
        err_quit("can't join with thread 1: %s\n", strerror(err));
    printf("thread 1 exit code %d\n", (int)tret);
    err = pthread_join(tid2, &tret);
    if (err != 0)
        err_quit("can't join with thread 2: %s\n", strerror(err));
    printf("thread 2 exit code %d\n", (int)tret);
    exit(0);
}

in that program, although pop functions has zero as an argument, the pushed functions are excuted( "thread 2 first handler", "thread 2 second handler", are printed by cleanup function)

why pop works even though put 0 for argument?

did I get pthread_cleanup_pop wrong??

解决方案

did I get pthread_cleanup_pop wrong?

No, you didn't.

The reason your code executed cleanup handlers, is that your control never reaches pthread_cleanup_pop(0). Instead, you always execute return in thr_fn1, and pthread_exit in thr_fn2.

Try creating the threads with pthread_create(&tid1, NULL, thr_fn1, (void *)0); instead of 1. When I do that, I get (expected):

thread 1 start
thread 1 push complete
thread 2 start
thread 2 push complete
thread 1 exit code 1
thread 2 exit code 2

这篇关于与参数0 pthread_cleanup_pop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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