linux多线程编程 同步与互斥

——本文一个例子展开,介绍Linux下面线程的操作、多线程的同步和互斥。

前言

线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步、互斥,这些东西将在本文中介绍。我在某QQ群里见到这样一道面试题:

是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:

1)有一int型全局变量g_Flag初始值为0;

2) 在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1

3) 在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2

4) 线程序1需要在线程2退出后才能退出

5) 主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出

我们带着这题开始这篇文章,结束之后,大家就都会做了。本文的框架如下:

  • 1、进程与线程
  • 2、使用线程的理由
  • 3、有关线程操作的函数
  • 4、线程之间的互斥
  • 5、线程之间的同步
  • 6、试题最终代码

1、进程与线程

进程是程序执行时的一个实例,即它是程序已经执行到何种程度的数据结构的汇集。从内核的观点看,进程的目的就是担当分配系统资源(CPU时间、内存等)的基本单位

线程是进程的一个执行流,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。一个进程由几个线程组成(拥有很多相对独立的执行流的用户程序共享应用程序的大部分数据结构),线程与同属一个进程的其他的线程共享进程所拥有的全部资源。

"进程——资源分配的最小单位,线程——程序执行的最小单位"

进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。线程有自己的堆栈和局部变量,但线程没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些。但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。

2、使用线程的理由

从上面我们知道了进程与线程的区别,其实这些区别也就是我们使用线程的理由。总的来说就是:进程有独立的地址空间,线程没有单独的地址空间(同一进程内的线程共享进程的地址空间)。(下面的内容摘自Linux下的多线程编程

使用多线程的理由之一是和进程相比,它是一种非常"节俭"的多任务操作方式。我们知道,在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种"昂贵"的多任务工作方式。而运行于一个进程中的多个线程,它们彼此之间使用相同的地址空间,共享大部分数据,启动一个线程所花费的空间远远小于启动一个进程所花费的空间,而且,线程间彼此切换所需的时间也远远小于进程间切换所需要的时间。据统计,总的说来,一个进程的开销大约是一个线程开销的30倍左右,当然,在具体的系统上,这个数据可能会有较大的区别。

使用多线程的理由之二是线程间方便的通信机制。对不同进程来说,它们具有独立的数据空间,要进行数据的传递只能通过通信的方式进行,这种方式不仅费时,而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。当然,数据的共享也带来其他一些问题,有的变量不能同时被两个线程所修改,有的子程序中声明为static的数据更有可能给多线程程序带来灾难性的打击,这些正是编写多线程程序时最需要注意的地方。

除了以上所说的优点外,不和进程比较,多线程程序作为一种多任务、并发的工作方式,当然有以下的优点:

  • 提高应用程序响应。这对图形界面的程序尤其有意义,当一个操作耗时很长时,整个系统都会等待这个操作,此时程序不会响应键盘、鼠标、菜单的操作,而使用多线程技术,将耗时长的操作(time consuming)置于一个新的线程,可以避免这种尴尬的情况。
  • 使多CPU系统更加有效。操作系统会保证当线程数不大于CPU数目时,不同的线程运行于不同的CPU上。
  • 改善程序结构。一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。

从函数调用上来说,进程创建使用fork()操作;线程创建使用clone()操作。

3、有关线程操作的函数

  1. #include <pthread.h>   
  2.    
  3. int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func) (void *), void *arg);  
  4. int pthread_join (pthread_t tid, void ** status);  
  5. pthread_t pthread_self (void);  
  6. int pthread_detach (pthread_t tid);  
  7. void pthread_exit (void *status);  
  8. <SPAN style="FONT-FAMILY: Arial; BACKGROUND-COLOR: #ffffff"></SPAN>  
#include <pthread.h>
 
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func) (void *), void *arg);
int pthread_join (pthread_t tid, void ** status);
pthread_t pthread_self (void);
int pthread_detach (pthread_t tid);
void pthread_exit (void *status);
pthread_create用于创建一个线程,成功返回 0,否则返回 -1。
  • pthread_t *tid:线程id的类型为pthread_t,通常为无符号整型,当调用pthread_create成功时,通过*tid指针返回。
  • const pthread_attr_t *attr:指定创建线程的属性,如线程优先级、初始栈大小、是否为守护进程等。可以使用NULL来使用默认值,通常情况下我们都是使用默认值。
  • void *(*func) (void *):函数指针func,指定当新的线程创建之后,将执行的函数。
  • void *arg:线程将执行的函数的参数。如果想传递多个参数,请将它们封装在一个结构体中。

pthread_join用于等待某个线程退出,成功返回 0,否则返回 -1。

  • pthread_t tid:指定要等待的线程ID
  • void ** status:如果不为NULL,那么线程的返回值存储在status指向的空间中(这就是为什么status是二级指针的原因!这种才参数也称为“值-结果”参数)。

pthread_self用于返回当前线程的ID。

pthread_detach用于是指定线程变为分离状态,就像进程脱离终端而变为后台进程类似。成功返回 0,否则返回 -1。变为分离状态的线程,如果线程退出,它的所有资源将全部释放。而如果不是分离状态,线程必须保留它的线程ID,退出状态直到其它线程对它调用了pthread_join

进程也是类似,这也是当我们打开进程管理器的时候,发现有很多僵死进程的原因!也是为什么一定要有僵死这个进程状态。

pthread_exit用于终止线程,可以指定返回值,以便其他线程通过pthread_join函数获取该线程的返回值。

  • void *status:指针线程终止的返回值。

知道了这些函数之后,我们试图来完成本文一开始的问题:

1)有一int型全局变量g_Flag初始值为0;

2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1

3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2

这3点很简单嘛!!!不就是调用pthread_create创建线程。代码如下:

这样就完成了1)、2)、3)这三点要求。编译执行得如下结果:
  1. /* 
  2.  * 1)有一int型全局变量g_Flag初始值为0; 
  3.  * 
  4.  * 2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1 
  5.  * 
  6.  * 3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2 
  7.  * 
  8.  */  
  9. #include<stdio.h>   
  10. #include<stdlib.h>   
  11. #include<pthread.h>   
  12. #include<errno.h>   
  13. #include<unistd.h>   
  14.   
  15. int g_Flag=0;  
  16.   
  17. void* thread1(void*);  
  18. void* thread2(void*);  
  19.   
  20. /* 
  21.  * when program is started, a single thread is created, called the initial thread or main thread. 
  22.  * Additional threads are created by pthread_create. 
  23.  * So we just need to create two thread in main(). 
  24.  */  
  25. int main(int argc, char** argv)  
  26. {  
  27.     printf("enter main\n");  
  28.     pthread_t tid1, tid2;  
  29.     int rc1=0, rc2=0;  
  30.     rc2 = pthread_create(&tid2, NULL, thread2, NULL);  
  31.     if(rc2 != 0)  
  32.         printf("%s: %d\n",__func__, strerror(rc2));  
  33.   
  34.     rc1 = pthread_create(&tid1, NULL, thread1, &tid2);  
  35.     if(rc1 != 0)  
  36.         printf("%s: %d\n",__func__, strerror(rc1));  
  37.     printf("leave main\n");  
  38.     exit(0);      
  39. }  
  40. /* 
  41.  * thread1() will be execute by thread1, after pthread_create() 
  42.  * it will set g_Flag = 1; 
  43.  */  
  44. void* thread1(void* arg)  
  45. {  
  46.     printf("enter thread1\n");  
  47.     printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());  
  48.     g_Flag = 1;  
  49.     printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());  
  50.     printf("leave thread1\n");  
  51.     pthread_exit(0);  
  52. }  
  53.   
  54. /* 
  55.  * thread2() will be execute by thread2, after pthread_create() 
  56.  * it will set g_Flag = 2; 
  57.  */  
  58. void* thread2(void* arg)  
  59. {  
  60.     printf("enter thread2\n");  
  61.     printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());  
  62.     g_Flag = 2;  
  63.     printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());  
  64.     printf("leave thread2\n");  
  65.     pthread_exit(0);  
  66. }  
/*
 * 1)有一int型全局变量g_Flag初始值为0;
 *
 * 2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1
 *
 * 3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2
 *
 */
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>

int g_Flag=0;

void* thread1(void*);
void* thread2(void*);

/*
 * when program is started, a single thread is created, called the initial thread or main thread.
 * Additional threads are created by pthread_create.
 * So we just need to create two thread in main().
 */
int main(int argc, char** argv)
{
	printf("enter main\n");
	pthread_t tid1, tid2;
	int rc1=0, rc2=0;
	rc2 = pthread_create(&tid2, NULL, thread2, NULL);
	if(rc2 != 0)
		printf("%s: %d\n",__func__, strerror(rc2));

	rc1 = pthread_create(&tid1, NULL, thread1, &tid2);
	if(rc1 != 0)
		printf("%s: %d\n",__func__, strerror(rc1));
	printf("leave main\n");
	exit(0);	
}
/*
 * thread1() will be execute by thread1, after pthread_create()
 * it will set g_Flag = 1;
 */
void* thread1(void* arg)
{
	printf("enter thread1\n");
	printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
	g_Flag = 1;
	printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
	printf("leave thread1\n");
	pthread_exit(0);
}

/*
 * thread2() will be execute by thread2, after pthread_create()
 * it will set g_Flag = 2;
 */
void* thread2(void* arg)
{
	printf("enter thread2\n");
	printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
	g_Flag = 2;
	printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
	printf("leave thread2\n");
	pthread_exit(0);
}

netsky@ubuntu:~/workspace/pthead_test$ gcc -lpthread test.c

如果程序中使用到了pthread库中的函数,除了要#include<pthread.h>,在编译的时候还有加上-lpthread 选项。
netsky@ubuntu:~/workspace/pthead_test$ ./a.out
enter main
enter thread2
this is thread2, g_Flag: 0, thread id is 3079588720
this is thread1, g_Flag: 2, thread id is 3079588720
leave thread2
leave main
enter thread1
this is thread1, g_Flag: 2, thread id is 3071196016
this is thread1, g_Flag: 1, thread id is 3071196016
leave thread1
但是运行结果不一定是上面的,还有可能是:

netsky@ubuntu:~/workspace/pthead_test$ ./a.out
enter main
leave main
enter thread1
this is thread1, g_Flag: 0, thread id is 3069176688
this is thread1, g_Flag: 1, thread id is 3069176688
leave thread1

或者是:

netsky@ubuntu:~/workspace/pthead_test$ ./a.out
enter main
leave main
等等。这也很好理解因为,这取决于主线程main函数何时终止,线程thread1、thread2是否能够来得急执行它们的函数。这也是多线程编程时要注意的问题,因为有可能一个线程会影响到整个进程中的所有其它线程!如果我们在main函数退出前,sleep()一段时间,就可以保证thread1、thread2来得及执行。

吸血蝙蝠Attention:大家肯定已经注意到了,我们在线程函数thread1()、thread2()执行完之前都调用了pthread_exit。如果我是调用exit()又或者是return会怎样呢?自己动手试试吧!

pthread_exit()用于线程退出,可以指定返回值,以便其他线程通过pthread_join()函数获取该线程的返回值。
return是函数返回,只有线程函数return,线程才会退出。
exit是进程退出,如果在线程函数中调用exit,进程中的所有函数都会退出!

“4) 线程序1需要在线程2退出后才能退出”第4点也很容易解决,直接在thread1的函数退出之前调用pthread_join就OK了。

4、线程之间的互斥

上面的代码似乎很好的解决了问题的前面4点要求,其实不然!!!因为g_Flag是一个全局变量,线程thread1和thread2可以同时对它进行操作,需要对它进行加锁保护,thread1和thread2要互斥访问才行。下面我们就介绍如何加锁保护——互斥锁。

互斥锁:

         使用互斥锁(互斥)可以使线程按顺序执行。通常,互斥锁通过确保一次只有一个线程执行代码的临界段来同步多个线程。互斥锁还可以保护单线程代码。

    mutex 是一种简单的加锁的方法来控制对共享资源的存取。这个互斥锁只有两种状态,也就是上锁和解锁,可以把互斥锁看作某种意义上的全局变量。在同一时刻只能有一个线程掌握某个互斥上的锁,拥有上锁状态的线程能够对共享资源进行操作。若其他线程希望上锁一个已经上锁了的互斥锁,则该线程就会挂起,直到上锁的线程释放掉互斥锁为止。可以说,这把互斥锁使得共享资源按序在各个线程中操作。 
互斥锁的操作主要包括以下几个步骤。 
·  互斥锁初始化:pthread_mutex_init 
·  互斥锁上锁:pthread_mutex_lock 
·  互斥锁判断上锁:pthread_mutex_trylock 
·  互斥锁接锁:pthread_mutex_unlock 
·  消除互斥锁:pthread_mutex_destroy 
其中,互斥锁可以分为快速互斥锁、递归互斥锁和检错互斥锁。这三种锁的区别主要在于其他未占有互斥锁的线程在希望得到互斥锁时的是否需要阻塞等待。快速锁是指调用线程会阻塞直至拥有互斥锁的线程解锁为止。递归互斥锁能够成功地返回并且增加调用线程在互斥上加锁的次数,而检错互斥锁则为快速互斥锁的非阻塞版本,它会立即返回并返回一个错误信息。 

互斥锁的相关操作函数如下:

  1. #include <pthread.h>    
  2.   
  3. int pthread_mutex_lock(pthread_mutex_t * mptr);   
  4. int pthread_mutex_unlock(pthread_mutex_t * mptr);   
  5. //Both return: 0 if OK, positive Exxx value on error  
#include <pthread.h> 

int pthread_mutex_lock(pthread_mutex_t * mptr); 
int pthread_mutex_unlock(pthread_mutex_t * mptr); 
//Both return: 0 if OK, positive Exxx value on error

在对临界资源进行操作之前需要 pthread_mutex_lock先加锁,操作完之后 pthread_mutex_unlock再解锁。而且在这之前需要声明一个 pthread_mutex_t类型的变量,用作前面两个函数的参数。具体代码见第5节。

5、线程之间的同步

第5点——主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出。就需要用到线程同步技术!线程同步需要条件变量。

条件变量:

使用条件变量可以以原子方式阻塞线程,直到某个特定条件为真为止。条件变量始终与互斥锁一起使用。对条件的测试是在互斥锁(互斥)的保护下进行的。

如果条件为假,线程通常会基于条件变量阻塞,并以原子方式释放等待条件变化的互斥锁。如果另一个线程更改了条件,该线程可能会向相关的条件变量发出信号,从而使一个或多个等待的线程执行以下操作:

  • 唤醒
  • 再次获取互斥锁
  • 重新评估条件

在以下情况下,条件变量可用于在进程之间同步线程:

  • 线程是在可以写入的内存中分配的
  • 内存由协作进程共享

使用条件变量可以以原子方式阻塞线程,直到某个特定条件为真为止。”即可用到第5点,主线程main函数阻塞于等待g_Flag从1变为2,或者从2变为1。条件变量的相关函数如下:

  1. #include <pthread.h>   
  2.    
  3. int pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr);   
  4. int pthread_cond_signal(pthread_cond_t *cptr);   
  5. //Both return: 0 if OK, positive Exxx value on error  
#include <pthread.h>
 
int pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr); 
int pthread_cond_signal(pthread_cond_t *cptr); 
//Both return: 0 if OK, positive Exxx value on error

pthread_cond_wait用于等待某个特定的条件为真, pthread_cond_signal用于通知阻塞的线程某个特定的条件为真了。在调用者两个函数之前需要声明一个 pthread_cond_t类型的变量,用于这两个函数的参数。

为什么条件变量始终与互斥锁一起使用,对条件的测试是在互斥锁(互斥)的保护下进行的呢?因为“某个特性条件”通常是在多个线程之间共享的某个变量。互斥锁允许这个变量可以在不同的线程中设置和检测。

通常,pthread_cond_wait只是唤醒等待某个条件变量的一个线程。如果需要唤醒所有等待某个条件变量的线程,需要调用:

int pthread_cond_broadcast (pthread_cond_t * cptr);

默认情况下面,阻塞的线程会一直等待,知道某个条件变量为真。如果想设置最大的阻塞时间可以调用:

int pthread_cond_timedwait (pthread_cond_t * cptr, pthread_mutex_t *mptr, const struct timespec *abstime);

如果时间到了,条件变量还没有为真,仍然返回,返回值为ETIME。

6、试题最终代码

通过前面的介绍,我们可以轻松的写出代码了,如下所示:

  1. /* 
  2.  是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能: 
  3.   1)有一int型全局变量g_Flag初始值为0; 
  4.   2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1 
  5.   3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2 
  6.   4)线程序1需要在线程2退出后才能退出 
  7.   5)主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出 
  8.    */  
  9. #include<stdio.h>   
  10. #include<stdlib.h>   
  11. #include<pthread.h>   
  12. #include<errno.h>   
  13. #include<unistd.h>   
  14.   
  15. typedef void* (*fun)(void*);  
  16.   
  17. int g_Flag=0;  
  18. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  
  19. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;  
  20.   
  21. void* thread1(void*);  
  22. void* thread2(void*);  
  23.   
  24. /* 
  25.  *  when program is started, a single thread is created, called the initial thread or main thread. 
  26.  *  Additional threads are created by pthread_create. 
  27.  *  So we just need to create two thread in main(). 
  28.  */  
  29.   
  30. int main(int argc, char** argv)  
  31. {  
  32.     printf("enter main\n");  
  33.     pthread_t tid1, tid2;  
  34.     int rc1=0, rc2=0;  
  35.     rc2 = pthread_create(&tid2, NULL, thread2, NULL);  
  36.     if(rc2 != 0)  
  37.         printf("%s: %d\n",__func__, strerror(rc2));  
  38.   
  39.     rc1 = pthread_create(&tid1, NULL, thread1, &tid2);  
  40.     if(rc1 != 0)  
  41.         printf("%s: %d\n",__func__, strerror(rc1));  
  42.   
  43.     pthread_cond_wait(&cond, &mutex);  
  44.     printf("leave main\n");  
  45.     exit(0);      
  46. }  
  47.   
  48. /* 
  49.  * thread1() will be execute by thread1, after pthread_create() 
  50.  * it will set g_Flag = 1; 
  51.  */  
  52. void* thread1(void* arg)  
  53. {  
  54.     printf("enter thread1\n");  
  55.     printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());  
  56.     pthread_mutex_lock(&mutex);  
  57.     if(g_Flag == 2)  
  58.         pthread_cond_signal(&cond);  
  59.     g_Flag = 1;  
  60.     printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());  
  61.     pthread_mutex_unlock(&mutex);  
  62.     pthread_join(*(pthread_t*)arg, NULL);  
  63.     printf("leave thread1\n");  
  64.     pthread_exit(0);  
  65. }  
  66.   
  67. /* 
  68.  * thread2() will be execute by thread2, after pthread_create() 
  69.  * it will set g_Flag = 2; 
  70.  */  
  71. void* thread2(void* arg)  
  72. {  
  73.     printf("enter thread2\n");  
  74.     printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());  
  75.     pthread_mutex_lock(&mutex);  
  76.     if(g_Flag == 1)  
  77.         pthread_cond_signal(&cond);  
  78.     g_Flag = 2;  
  79.     printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());  
  80.     pthread_mutex_unlock(&mutex);  
  81.     printf("leave thread2\n");  
  82.     pthread_exit(0);  
  83. }  
  84.   
  85. 编译运行可以得到符合要求的结果  
/*
 是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:
  1)有一int型全局变量g_Flag初始值为0;
  2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1
  3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2
  4)线程序1需要在线程2退出后才能退出
  5)主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出
   */
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>

typedef void* (*fun)(void*);

int g_Flag=0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* thread1(void*);
void* thread2(void*);

/*
 *  when program is started, a single thread is created, called the initial thread or main thread.
 *  Additional threads are created by pthread_create.
 *  So we just need to create two thread in main().
 */

int main(int argc, char** argv)
{
	printf("enter main\n");
	pthread_t tid1, tid2;
	int rc1=0, rc2=0;
	rc2 = pthread_create(&tid2, NULL, thread2, NULL);
	if(rc2 != 0)
		printf("%s: %d\n",__func__, strerror(rc2));

	rc1 = pthread_create(&tid1, NULL, thread1, &tid2);
	if(rc1 != 0)
		printf("%s: %d\n",__func__, strerror(rc1));

	pthread_cond_wait(&cond, &mutex);
	printf("leave main\n");
	exit(0);	
}

/*
 * thread1() will be execute by thread1, after pthread_create()
 * it will set g_Flag = 1;
 */
void* thread1(void* arg)
{
	printf("enter thread1\n");
	printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
	pthread_mutex_lock(&mutex);
	if(g_Flag == 2)
		pthread_cond_signal(&cond);
	g_Flag = 1;
	printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
	pthread_mutex_unlock(&mutex);
	pthread_join(*(pthread_t*)arg, NULL);
	printf("leave thread1\n");
	pthread_exit(0);
}

/*
 * thread2() will be execute by thread2, after pthread_create()
 * it will set g_Flag = 2;
 */
void* thread2(void* arg)
{
	printf("enter thread2\n");
	printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
	pthread_mutex_lock(&mutex);
	if(g_Flag == 1)
		pthread_cond_signal(&cond);
	g_Flag = 2;
	printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
	pthread_mutex_unlock(&mutex);
	printf("leave thread2\n");
	pthread_exit(0);
}

编译运行可以得到符合要求的结果

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值