天嵌 ARM开发社区

 找回密码
 注册
查看: 1003|回复: 0

linux清理函数问题

[复制链接]
溪孟 发表于 2013-11-15 20:14:27 | 显示全部楼层 |阅读模式
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>

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

void *Thr1( void *arg )
{
    pthread_cleanup_push( CleanUp, "thread 1 cleanup done\n" );
    printf( "thread 1 push done\n" );
    if( arg )
        pthread_exit( (void *)2 );
    else
        return ( (void *)1 );
    pthread_cleanup_pop( 0 );
}
void *Thr2( void *arg )
{
    pthread_cleanup_push( CleanUp, "thread 1 cleanup done\n" );
    printf( "thread 1 push done\n" );
    if( arg );
        pthread_cleanup_pop( 0 );
    else
        pthread_cleanup_pop( 1
);
    pthread_exit( (void *)2 );
}
int main( int argc, char **argv )
{
    pthread_t     tid1, tid2, tid3, tid4;
    void     *tret;

    pthread_create( &tid1, NULL, Thr1, (void *)0 );
    pthread_create( &tid2, NULL, Thr1, (void *)1 );
    pthread_create( &tid3, NULL, Thr2, (void *)0 );
    pthread_create( &tid4, NULL, Thr2, (void *)1 );
    pthread_join( tid1, &tret );
    pthread_join( tid2, &tret );
    pthread_join( tid3, &tret );
    pthread_join( tid4, &tret );

    exit( 0 );

}
程序编译不通过,出错原因是Thr2中if语句那一部分有问题,但是从push和pop要匹配使用的要求来看,Thr2的却只会执行pthread_cleanup_pop( 0 );或者pthread_cleanup_pop( 1 );那为什么会出错呢?求高人解答
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-9-28 13:25 , Processed in 1.027383 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表