多线程编程2

线程终止: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 );
}
问题:
对于线程的执行顺序还有一定迷惑:要是操作系统采用时间片论转调度,那么创建的线程与原线程的优先级关系如何?如果优先级相等,那么感觉运行哪个线程便是随机的,但从运行结果来看,主线程创建线程后貌似都是继续向下执行。(主线程创建线程后在没有等待类型的函数就会一直向下执行,不会执行创建的线程(为什么?待查))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值