多线程编程2
来源:互联网 发布:c语言 ip地址 合法性 编辑:程序博客网 时间:2024/05/29 21:33
线程终止: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( "getin tid1\n" );
sleep( 1);
printf("thread 1 returning\n" );
return((void *)1);
}
void *thr_fn2( void *arg )
{
printf( "getin 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 )
{
}
问题:
对于线程的执行顺序还有一定迷惑:要是操作系统采用时间片论转调度,那么创建的线程与原线程的优先级关系如何?如果优先级相等,那么感觉运行哪个线程便是随机的,但从运行结果来看,主线程创建线程后貌似都是继续向下执行。(主线程创建线程后在没有等待类型的函数就会一直向下执行,不会执行创建的线程(为什么?待查))
0 0
- 多线程编程指南2--用多线程编程
- 多线程编程杂说2
- delphi多线程编程2
- VC多线程编程2
- 多线程编程2
- MFC多线程编程2
- 多线程编程2 - NSOperation
- windows多线程编程2
- 多线程编程2 - NSOperation
- 多线程编程2-NSOperation
- 多线程编程2 - NSOperation
- 多线程编程2 - NSOperation
- 多线程编程2
- Linux多线程编程(2)
- 多线程编程2 - NSOperation
- 多线程编程2 - NSOperation
- 多线程编程2 - NSOperation
- 多线程编程2 - NSOperation
- vi 快捷键(作者VPSer)
- 共享内存
- 消息队列
- Sky数
- 多线程编程1
- 多线程编程2
- 多线程编程3
- TCP网络编程
- ARM工具ADS
- ubuntu nfs
- 环境变量的设置
- 关于keil编译51单片机(1<<16)运…
- (转载) 关于nor flash,nand flash…
- linux samba服务器设置