多线程编程实例---pthread_join函数详解1

来源:互联网 发布:坏种 知乎 编辑:程序博客网 时间:2024/06/07 09:46

 

单处理器上的linux多线程,是通过分时操作完成的;
此时互斥锁的作用,只有在时间足够的情况下才能体现出来,即有时线程内需要延时;
否则只有第一个线程不断解锁和获锁,别的线程在第一个线程执行完前无法获得互斥锁。

三 pthread_join pthread_exit
  
函数pthread_join用来等待一个线程的结束。函数原型为:
  extern int pthread_join __P ((pthread_t __th, void **__thread_return));
  第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。它的函数原型为:
  extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
  唯一的参数是函数的返回代码,只要pthread_join中的第二个参数thread_return不是NULL,这个值将被传递给 thread_return。最后要说明的是,一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。
  在这一节里,我们编写了一个最简单的线程,并掌握了最常用的三个函数pthread_create,pthread_join和pthread_exit。下面,我们来了解线程的一些常用属性以及如何设置这些属性。

///////////////////////////////////////////////////////////////////////////

源程序:

/*thread_example.c : c multiple thread programming in linux
*/
#include
#include
#include
#include
#define MAX1 10
#define MAX2 30

pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;

void *thread1()
{
printf ("thread1 : I'm thread 1\n");

for (i = 0; i < MAX1; i++)
{
printf("thread1 : number = %d i=%d\n",number,i);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(2);
}


printf("thread1 :Is main function waiting for me acomplishing task? \n");
pthread_exit(NULL);
}

void *thread2()
{
printf("thread2 : I'm thread 2\n");

for (i = 0; i < MAX2; i++)
{
printf("thread2 : number = %d i=%d\n",number,i);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(3);
}


printf("thread2 :Is main function waiting for me to acomplish task ?\n");
pthread_exit(NULL);
}

void thread_create(void)
{
int temp;
memset(&thread, 0, sizeof(thread)); //comment1
/*创建线程*/
if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
printf("线程1创建失败!\n");
else
printf("Thread 1 is established\n");

if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3
printf("线程2创建失败");
else
printf("Thread 2 is established\n");
}

void thread_wait(void)
{
/*等待线程结束*/
if(thread[0] !=0) { //comment4
pthread_join(thread[0],NULL);
printf("Thread 1 is over \n");
}
if(thread[1] !=0) { //comment5
pthread_join(thread[1],NULL);
printf("Thread 2 is over\n");
}
}

int main()
{
/*用默认属性初始化互斥锁*/
pthread_mutex_init(&mut,NULL);

printf("I am the main funtion,and I am establishing threads. Ha-ha\n");
thread_create();
printf("I am the main funtion,and I am waiting for thread to accomplish task. Ha-ha\n");
thread_wait();

return 0;
}

///////////////////////////////////////////////////////////

执行情况1(linux终端):

[root@localhost root]# gcc -o joint joint.c -lpthread
[root@localhost root]# ./joint
I am the main funtion,and I am establishing threads. Ha-ha
thread1 : I'm thread 1
thread1 : number = 0 i=0
Thread 1 is established
thread2 : I'm thread 2
thread2 : number = 1 i=0
Thread 2 is established
I am the main funtion,and I am waiting for thread to accomplish task. Ha-ha
thread1 : number = 2 i=1
thread2 : number = 3 i=2
thread1 : number = 4 i=3
thread2 : number = 5 i=4
thread1 : number = 6 i=5
thread1 : number = 7 i=6
thread2 : number = 8 i=7
thread1 : number = 9 i=8
thread2 : number = 10 i=9
thread1 :Is main function waiting for me acomplishing task?
Thread 1 is over
thread2 : number = 11 i=11
thread2 : number = 12 i=12
thread2 : number = 13 i=13
thread2 : number = 14 i=14
thread2 : number = 15 i=15
thread2 : number = 16 i=16
thread2 : number = 17 i=17
thread2 : number = 18 i=18
thread2 : number = 19 i=19
thread2 : number = 20 i=20
thread2 : number = 21 i=21
thread2 : number = 22 i=22
thread2 : number = 23 i=23
thread2 : number = 24 i=24
thread2 : number = 25 i=25
thread2 : number = 26 i=26
thread2 : number = 27 i=27
thread2 : number = 28 i=28
thread2 : number = 29 i=29
thread2 :Is main function waiting for me to acomplish task ?
Thread 2 is over
[root@localhost root]#

///////////////////////////////////////////////////////////

执行情况2(linux终端)(去掉sleep(2)和sleep(3)):

[root@localhost zanshi7]# gcc -o joint joint.c -lpthread
[root@localhost zanshi7]# ./joint
I am the main funtion,and I am establishing threads. Ha-ha
thread1 : I'm thread 1
thread1 : number = 0 i=0
thread1 : number = 1 i=1
thread1 : number = 2 i=2
thread1 : number = 3 i=3
thread1 : number = 4 i=4
thread1 : number = 5 i=5
thread1 : number = 6 i=6
thread1 : number = 7 i=7
thread1 : number = 8 i=8
thread1 : number = 9 i=9
thread1 :Is main function waiting for me acomplishing task?
Thread 1 is established
thread2 : I'm thread 2
thread2 : number = 10 i=0
thread2 : number = 11 i=1
thread2 : number = 12 i=2
thread2 : number = 13 i=3
thread2 : number = 14 i=4
thread2 : number = 15 i=5
thread2 : number = 16 i=6
thread2 : number = 17 i=7
thread2 : number = 18 i=8
thread2 : number = 19 i=9
thread2 : number = 20 i=10
thread2 : number = 21 i=11
thread2 : number = 22 i=12
thread2 : number = 23 i=13
thread2 : number = 24 i=14
thread2 : number = 25 i=15
thread2 : number = 26 i=16
thread2 : number = 27 i=17
thread2 : number = 28 i=18
thread2 : number = 29 i=19
thread2 : number = 30 i=20
thread2 : number = 31 i=21
thread2 : number = 32 i=22
thread2 : number = 33 i=23
thread2 : number = 34 i=24
thread2 : number = 35 i=25
thread2 : number = 36 i=26
thread2 : number = 37 i=27
thread2 : number = 38 i=28
thread2 : number = 39 i=29
thread2 :Is main function waiting for me to acomplish task ?
Thread 2 is established
I am the main funtion,and I am waiting for thread to accomplish task. Ha-ha
Thread 1 is over
Thread 2 is over
[root@localhost zanshi7]#

linux的时间片可能是210ms.

但是,在《Linux系统分析与高级编程技术》一书的第20章高级线程编程的第217页,由下面的一段话:

这样是否达到了我们的要求了呢?不尽如此,因为依靠时间的延迟执行同步是不可靠的。
这里遇到的情形和一个分布程序和共享资源的情形一样。共享的资源是标准的输出设备,分布
计算的程序是三个线程。
其实这里还有另外一个错误。函数s l e e p和函数e x i t一样和进程有关。当线程调用s l e e p时,
整个的进程都处于睡眠状态,也就是说,所有的三个线程都进入睡眠状态。这样我们实际上没
有解决任何的问题。希望使一个线程睡眠的函数是p t h r e a d _ d e l a y _ n p。例如让一个线程睡眠2秒
钟,用如下程序:
struct timespec delay;
d e l a y.tv_sec = 2;
d e l a y.tv_nsec = 0;
pthread_delay_np( &delay );

但pthread_delay_np函数在solaris版本才出现,red hat9还没有。red hat9用pthread_cond_wait和pthread_cond_signal可能能勉强完成任务。所以要想彻底解决问题,只有采用更新更完善的linux。

原创粉丝点击