控制线程 ,简称线程。
典型的UNIX进程可以看成只有一个控制线程:一个线程在同时只做一件事。有了控制线程后,在程序设计时成在同一时刻做不止一件事情。每个线程各自独立的任务。这种方法的有以下好处:
1、简化异步事件代码
2、多线程自动访问相同的存储空间和文件描述符
3、吞吐量
4响应时间。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_t ntid;
void printids( char *s )
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf( "ntid = %u\n",(unsigned int)ntid );
printf( "%s pid %u tid %u (0x%x)\n ",s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid );
}
void *thr_fn( void *arg )
{
printids( "nes thread: " );
(void *)0;
}
int main(void)
{
int errno;
errno = pthread_create( &ntid, NULL, thr_fn, NULL ) ;
if( errno != 0 )
printf( "pthread_create error!\n" );
printids( "main thread: " );
sleep( 1 );
exit( 0 );
}
在UNIX apue 那本书上有个小错误。 应该为void * (*strt_rtn) (void *)
main thread 和new thread的 先后顺序不同。