多线程:线程的返回retrun和pthread_exit

/******************************************************************************************************************
参考:http://www.programfan.com/CLUB/showpost.asp?id=985
说明:线程的返回retrun和pthread_exit?。
******************************************************************************************************************/ 
其实还有除了上边这种还有一种是 异常 退出,这种要在线程中故意访问一个受系统保护的指针来验证一个。说区别干说多没有意思只有拿线程中用到的函数来找他们的区别,由于都是返回的先看看pthread_join这个函数。它俩会对会有什么不同,执行下边这个函数和把pthread_exit换成return,pthread_join接收到的值都是一样的。

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

int a = 1;

void *create(void *arg)
{
        printf("new pthread .. \n");
        printf("a = %d \n", a);
        pthread_exit ((void *)8);
}

int main(int argc, char *argv[])
{
        pthread_t tidp;
        int error;
        void *temp;
        
        error = pthread_create(&tidp, NULL, create, NULL);
        printf("main thread!\n");
        
        if( error )
        {
                printf("thread is not created ... \n");
                return -1;
        }
        error = pthread_join(tidp, &temp);
        
        if( error )
        {
                printf("thread is not exit .. \n");
                return -2;
        }
        
        printf("new  is  exit code %d \n", (int )temp);
        return 0;
}

运行结果:
[root@localhost gcc]# ./a.out 
main thread!
new pthread .. 
a = 1 
new  is  exit code 8 
[root@localhost gcc]# 

pthread_exit()   return 都可以用pthread_join来接收返回值的:(pthread_exit ((void *) 8);改为return (void*)8;效果一样)



第二个测试就是phread_cleanup_push,在书上隐隐约约看到这个只支持 异常退出 和 pthread_exit,不理return 那就来试试了。如下程序:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *clean(void *arg)
{
	printf("clearup: %s\n", (char*)arg);
	return (void*)0;
}

void *thr_fn1(void *arg)
{
	printf("thread 1 start  \n");
	pthread_cleanup_push((void*)clean, "thread 1 first handler");
	pthread_cleanup_push((void*)clean, "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((void*)clean, "thread 2 first handler");
	pthread_cleanup_push((void*)clean, "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)
	{
		printf("main1:error...\n");
		return -1;
	}

	err = pthread_create(&tid2, NULL, thr_fn2, (void *)1);
	
	if(err != 0)
	{
		printf("main2:error...\n");
		return -1;
	}
	
	err = pthread_join(tid1, &tret);
	if(err != 0)
	{
		printf("main3:error ... \n");
		return -1;
	}
	printf("thread 1 exit code %d \n", (int)tret);
	
	err = pthread_join(tid2, &tret);
	
	if(err != 0)
	{
		printf("main4:error ...\n");
		return -1;
	}
	printf("thread 2 exit code %d \n", (int)tret);
	return 1;
}
运行结果:

[root@localhost gcc]# ./a.out 
thread 2 start 
thread 2 push complete 
clearup: thread 2 second handler
clearup: thread 2 first handler
thread 1 start  
thread 1 push complete 
thread 1 exit code 1 
thread 2 exit code 2 
[root@localhost gcc]# 

运行结果来看就不是不支持return,异常退出的测试就要把线程改为:

void *thr_fn2(void *arg)
{
	printf("thread 2 start \n");
	
	pthread_cleanup_push((void*)clean, "thread 2 first handler");
	pthread_cleanup_push((void*)clean, "thread 2 second handler");
	printf("thread 2 push complete \n");
	
	if(arg)
	{
		printf("我可能要死了。。\n");
		*b = "ahahhah";
		printf("我还活着!\n");
		pthread_exit((void *)2);
	}
	pthread_cleanup_pop(0);
	pthread_cleanup_pop(0);
	pthread_exit((void*)2);
}

运行结果:

[root@localhost gcc]# gcc test.c -lpthread -Wall
test.c: 在函数‘thr_fn2’中:
test.c:41: 警告:赋值时将指针赋给整数,未作类型转换
[root@localhost gcc]# ./a.out 
thread 2 start 
thread 2 push complete 
我可能要死了。。
段错误 (core dumped)
[root@localhost gcc]# 
从运行结果来看我看书没看仔细,异常退出是不支持的!多亏整理了blog,才能真正了解!







你可能感兴趣的:(linux,linux,linux,pthread_exit)