不正确的 pthread_exit参数

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>


struct foo {
	int a, b, c, d;
};

void printfoo(const char *s, const struct foo *fp )
{
	printf( "	%s\n", s );
	printf( "	structure at 0x%x\n", (unsigned int)fp );
	printf("	foo.a = %d\n", fp->a );
	printf("	foo.b = %d\n", fp->b);
	printf("	foo.c = %d\n", fp->c);
	printf("	foo.d = %d\n", fp->d);
}


void * thr_fn1( void * arg )
{
	printf( "thread 1 :ID is %u\n", (int)pthread_self() );	
	struct foo foo = { 100,82.93,14 }; 

	printfoo( "thread 1 : \n", &foo );
	pthread_exit( (void *)(&foo) );

}

void * thr_fn2( void * arg )
{
	printf( "thread 2 : ID is %u\n", (unsigned)(int)pthread_self() );
	pthread_exit( (void *)0 );  //exit
}

int main( void )
{
	int		err;
	pthread_t	tid1, tid2;
	struct foo	*fp;
	

	
	err = pthread_create( &tid1, NULL, thr_fn1, NULL );
	if( err != 0 )
		printf( "pthread_create 1\n" );

	err = pthread_join( tid1,  (void *)&fp );
	if( err != 0 )
		printf( "can't join with thread 1  %s\n", strerror( err ) );

	//sleep(1);  // why
	printf( "parent starting second thread\n" );
	err = pthread_create( &tid2, NULL, thr_fn2, NULL );
	if( err != 0 )
		printf( "can't create thread 2: %s\n", strerror( err ));

	sleep( 1 );  //must why ---主线程退出后,第二个线程也就退出了,所以没有打印 thr_fn2的内容;
	printfoo( "parent : \n", fp );
	exit( 0 );

}


运行结果如下所示:

wangkai@ubuntu:~/Test$ ./a.out 
thread 1 :ID is 3078667120
	thread 1 : 

	structure at 0xb780b380
	foo.a = 100
	foo.b = 82
	foo.c = 14
	foo.d = 0   //这里为什么是没有第三个参数呢??? 因为在上面的赋值操作中,将, 写成了 。 所以只有三个值赋值,最后一个默认为0
 parent starting second thread
thread 2 : ID is 3078667120
	parent : 

	structure at 0xb780b380
	foo.a = 16712292
	foo.b = -1216302188
	foo.c = 16719860
	foo.d = 16712300
wangkai@ubuntu:~/Test$ 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值