线程终止:void pthread_exit(void *rval_ptr);
调用进程阻塞直到指定进程结束:int pthread_join(pthread_t thread, void **rval_ptr);
可以用第二个函数获取线程的终止状态
#include<unistd.h>
#include<pthread.h>
void
*tret;
pthread_t
tid1, tid2;
void *thr_fn1( void *arg )
{
printf( "get in tid1\n" );
sleep( 1 );
printf("thread 1 returning\n" );
return ((void *)1);
}
void *thr_fn2( void *arg )
{
printf( "get in tid2\n" );
pthread_join( tid1, &tret );
printf( "%d\n", (int)tret );//输出线程1终止状态
printf( "thread 2 exiting\n" );
pthread_exit((void *)2);
}
int main( int argc, char **argv )
{
int
err;
printf( "create tid1\n" );
pthread_create( &tid1, NULL, thr_fn1, NULL );
printf( "create tid2\n" );
pthread_create( &tid2, NULL, thr_fn2, NULL );
pthread_join( tid2, &tret );
printf( "%d\n", (int)tret );//输出线程2终止状态
exit( 0 );
}
问题:
对于线程的执行顺序还有一定迷惑:要是操作系统采用时间片论转调度,那么创建的线程与原线程的优先级关系如何?如果优先级相等,那么感觉运行哪个线程便是随机的,但从运行结果来看,主线程创建线程后貌似都是继续向下执行。(主线程创建线程后在没有等待类型的函数就会一直向下执行,不会执行创建的线程(为什么?待查))
调用进程阻塞直到指定进程结束:int pthread_join(pthread_t thread, void **rval_ptr);
可以用第二个函数获取线程的终止状态
#include<unistd.h>
#include<pthread.h>
void
pthread_t
void *thr_fn1( void *arg )
{
}
void *thr_fn2( void *arg )
{
}
int main( int argc, char **argv )
{
}
问题:
对于线程的执行顺序还有一定迷惑:要是操作系统采用时间片论转调度,那么创建的线程与原线程的优先级关系如何?如果优先级相等,那么感觉运行哪个线程便是随机的,但从运行结果来看,主线程创建线程后貌似都是继续向下执行。(主线程创建线程后在没有等待类型的函数就会一直向下执行,不会执行创建的线程(为什么?待查))