不正确的 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$ 


 

你可能感兴趣的:(不正确的 pthread_exit参数)