本文共 15667 字,大约阅读时间需要 52 分钟。
http://blog.csdn.net/big_bit/article/details/51356393
在进行多线程编程时,难免还要碰到两个问题,那就线程间的互斥与同步:
线程同步是指线程之间所具有的一种制约关系,一个线程的执行依赖另一个线程的消息,当它没有得到另一个线程的消息时应等待,直到消息到达时才被唤醒。
线程互斥是指对于共享的进程系统资源,在各单个线程访问时的排它性。当有若干个线程都要使用某一共享资源时,任何时刻最多只允许一个线程去使用,其它要使用该资源的线程必须等待,直到占用资源者释放该资源。线程互斥可以看成是一种特殊的线程同步(下文统称为同步)。
生产者消费者问题就是一个著名的线程同步问题,该问题描述如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个具有多个缓冲区的缓冲池,生产者将它生产的产品放入一个缓冲区中,消费者可以从缓冲区中取走产品进行消费,显然生产者和消费者之间必须保持同步,即不允许消费者到一个空的缓冲区中取产品,也不允许生产者向一个已经放入产品的缓冲区中再次投放产品。
关于线程同步和互斥的详细说明可以看: http://blog.csdn.net/big_bit/article/details/51356381这篇文章
线程间的同步方法大体可分为两类:用户模式和内核模式。顾名思义,内核模式就是指利用系统内核对象的单一性来进行同步,使用时需要切换内核态与用户态,而用户模式就是不需要切换到内核态,只在用户态完成操作。
用户模式下的方法有:原子操作(例如一个单一的全局变量),临界区。内核模式下的方法有:事件,信号量,互斥量。下面我们来分别看一下这些方法:
一、互斥锁或互斥量(mutex)
下面是用互斥量来解决生产者和消费者问题。为了现集中体现互斥量这个概念(就是一次只能有一个线程访问,其他线程阻塞),我们先简化一下问题:缓冲区或者仓库无限大(生产者和消费者都可以生产和消费产品,而且产品初始化时候数量就是无限多,这里我们主要体现),只有一个生产者和一个消费者,我们这个时候就可以把缓冲区设置为一个互斥量,一次要么生产者要么消费者霸占它。 · 初始化锁。在Linux下,线程的互斥量数据类型是pthread_mutex_t。在使用前,要对它进行初始化。
静态分配:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
动态分配:int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutex_attr_t *mutexattr);
· 加锁。对共享资源的访问,要对互斥量进行加锁,如果互斥量已经上了锁,调用线程会阻塞,直到互斥量被解锁。int pthread_mutex_lock(pthread_mutex *mutex);int pthread_mutex_trylock(pthread_mutex_t *mutex);
· 解锁。在完成了对共享资源的访问后,要对互斥量进行解锁。int pthread_mutex_unlock(pthread_mutex_t *mutex);
· 销毁锁。锁在是使用完成后,需要进行销毁以释放资源。int pthread_mutex_destroy(pthread_mutex *mutex);
接下来我们来看看实现流程:
下面开始代码实现:
- #include <stdio.h>
- #include <pthread.h>
-
- #define LOOP_COUNT 5 //生产者和消费者各自循环次数
- pthread_mutex_t mutex;
-
-
- void *producer( void *arg );
- void *consumer( void *arg );
-
- int main(int argc , char *argv[]){
- pthread_t thrd_prod , thrd_cons;
-
- pthread_mutex_init( &mutex , NULL );
-
-
- if( pthread_create( &thrd_prod , NULL, producer ,
- NULL ) != 0 )
- oops( "thread create failed." );
- sleep(1);
-
- if( pthread_create( &thrd_cons , NULL, consumer ,
- NULL ) != 0 )
- oops( "thread create failed." );
-
-
- if( pthread_join( thrd_prod , NULL ) != 0 )
- oops( " wait thread failed.");
- if( pthread_join( thrd_cons , NULL ) != 0 )
- oops( " wait thread failed.");
-
- pthread_mutex_destroy( &mutex );
- return 0;
- }
-
- void *producer( void *arg){
- int count = 0 ;
-
- while( count++ < LOOP_COUNT ){
- pthread_mutex_lock( &mutex );
-
-
-
- printf( " producer put a product to buffer.\n");
- sleep(3);
-
- pthread_mutex_unlock( &mutex );
- sleep(1);
- }
- }
- void *consumer( void *arg ){
- int count = 0 ;
-
- while( count++ < LOOP_COUNT ){
-
- pthread_mutex_lock( &mutex );
-
-
-
- printf( " consumer get a product from buffer.\n");
-
- pthread_mutex_unlock( &mutex );
- sleep(1);
- }
- }
从结果可以看到,当生产者和消费者成功lock互斥量时,另一个就阻塞等待。 二、读写锁
读写锁也叫做共享-独占锁,当读写锁以读模式锁住时,它是以共享模式锁住的;当它以写模式锁住时,它是以独占模式锁住的。
接下来我们改变一下生产者消费者问题:现在缓冲区或者仓库无限大(生产者和消费者都可以生产和消费产品,而且产品初始化时候数量就是无限多,这里我们主要体现),只有一个生产者(读写锁也可以应用到多个生产者问题),但有多个消费者, 我们这个时候就可以把为生产者设置一个写锁,为每个消费者设置一个读锁。
- 1.初始化读写锁。
#include <pthread.h>int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,constpthread_rwlockattr_t *restrict attr);
2.加锁。要在读模式下锁定读写锁,需要调用pthread_rwlock_rdlock;要在写模式下锁定读写锁,需要调用pthread_rwlock_wrlock。当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞。当读写锁在读加锁状态时,所有试图以读模式对它进行加锁的线程都可以得到访问权,但是如果线程希望以写模式对此锁进行加锁,它必须阻塞直到所有的线程释放读锁。
intpthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
intpthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
3.解锁。在完成了对共享资源的访问后,要对读写锁进行解锁。int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
4.销毁锁。在释放读写锁占用的内存之前,需要调用pthread_rwlock_destroy做清理工作。如果pthread_rwlock_init为读写锁分配了资源,pthread_rwlock_destroy将释放这些资源。如果在调用pthread_rwlock_destroy之前就释放了读写锁占用的内存空间,那么分配给这个锁的资源就丢失了。int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
- #include <stdio.h>
- #include <pthread.h>
-
- #define LOOP_COUNT 2 //生产者和消费者各自循环次数
- #define LOOP_THRD 5 //消费者线程个数
- pthread_rwlock_t rwlock;
-
-
- void *producer( void *arg );
- void *consumer( void *arg );
-
- int main(int argc , char *argv[]){
- int thrd_num ,thrd_id[LOOP_THRD] ;
- pthread_t thrd_prod , thrd_cons[LOOP_THRD];
-
- pthread_rwlock_init( &rwlock , NULL );
-
-
- if( pthread_create( &thrd_prod , NULL, producer ,
- NULL ) != 0 )
- oops( "thread create failed." );
-
- for( thrd_num = 0 ; thrd_num < LOOP_THRD; thrd_num++ ){
- thrd_id[thrd_num] = thrd_num;
- if( pthread_create( &thrd_cons[thrd_num], NULL, consumer
- , <span style="background-color: rgb(255, 0, 0);">(void *)( thrd_id+thrd_num)</span> ) != 0 )
- oops( "thread %d create failed." , thrd_num );
- }
-
-
- if( pthread_join( thrd_prod , NULL ) != 0 )
- oops( " wait thread failed.");
- for( thrd_num = 0 ; thrd_num < LOOP_THRD; thrd_num++ ){
- if( pthread_join( thrd_cons[thrd_num] , NULL ) != 0 )
- oops( " wait thread %d failed." , thrd_num);
-
- }
- pthread_rwlock_destroy( &rwlock );
- return 0;
- }
-
- void *producer( void *arg){
- int count = 0 ;
-
- while( count++ < LOOP_COUNT ){
- printf( "producer try to lock wrlock.\n");
- pthread_rwlock_wrlock( &rwlock );
-
-
-
- printf( "producer lock successful, producer put a product to buffer.\n");
-
-
-
-
-
- sleep(3);
- printf("prducer finished ,unlock wrlock.\n");
- pthread_rwlock_unlock( &rwlock );
- sleep(1);
- }
- }
- void *consumer( void *arg ){
- int count = 0 ;
- int thrd_id = *( ( int*)arg );
-
-
- while( count++ < LOOP_COUNT ){
-
- printf( "consumer try to lock rdlock.\n" );
- pthread_rwlock_rdlock( &rwlock );
-
-
-
- printf( " consumer locked successful ,consumer %d get a product from buffer."
- "\n" , thrd_id);
-
-
-
-
- sleep(3);
- printf("consumer finished ,unlock rdlock.\n");
- pthread_rwlock_unlock( &rwlock );
- sleep(thrd_id+1);
- }
- }
结果如下:
可以看到当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞。当读写锁在读加锁状态时,所有试图以读模式对它进行加锁的线程都可以得到访问权,但是如果线程希望以写模式对此锁进行加锁,它必须阻塞直到所有的线程释放读锁。虽然读写锁的实现各不相同,但当读写锁处于读模式锁住状态时,如果有另外的线程试图以写模式加锁,读写锁通常会阻塞随后的读模式锁请求(貌似在程序里面没有体现出来)。这样可以避免读模式锁长期占用,而等待的写模式锁请求一直得不到满足。 另外我要说明的一点就是,传递参数 arg 为(void *)( thrd_id+thrd_num),我一开始并没有定义一个数组thrd_cons[LOOP_THRD]来存储线程编号的, 而是直接传thrd_num的地址,但通过在线程 int thrd_id = *( ( int*)arg );// printf( "consumer %d ,%#x . \n" , thrd_id ,arg); 这两句话就可以知道,当传递的是thrd_num地址时候,由于进程的所有信息对该进程的所有线程都是共享的,包括可执行的程序文本、程序的全局内存和堆内存、栈以及文件描述符。地址, 由于进程的所有信息对该进程的所有线程都是共享的,包括可执行的程序文本、程序的全局内存和堆内存、栈以及文件描述符。 thrd_num的值会随着线程的执行而发生改变,系统调度频率之快是我们无法想像的,所以thrd_num的值也是动态改变的。 三、条件变量(cond)
与互斥锁不同,条件变量是用来等待而不是用来上锁的。条件变量用来自动阻塞一个线程,直到某特殊情况发生为止。通常条件变量和互斥锁同时使用。条件变量分为两部分:条件和变量。条件本身是由互斥量保护的。线程在改变条件状态前先要锁住互斥量。条件变量使我们可以睡眠等待某种条件出现。条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。条件的检测是在互斥锁的保护下进行的。如果一个条件为假,一个线程自动阻塞,并释放等待状态改变的互斥锁。如果另一个线程改变了条件,它发信号给关联的条件变量,唤醒一个或多个等待它的线程,重新获得互斥锁,重新评价条件。如果两进程共享可读写的内存,条件变量可以被用来实现这两进程间的线程同步。
1.初始化条件变量。静态态初始化,pthread_cond_t cond = PTHREAD_COND_INITIALIER;
动态初始化,int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr);
2.等待条件成立。释放锁,同时阻塞等待条件变量为真才行。timewait()设置等待时间,仍未signal,返回ETIMEOUT(加锁保证只有一个线程wait)int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,consttimespec *abstime);
3.激活条件变量。pthread_cond_signal,pthread_cond_broadcast(激活所有等待线程)int pthread_cond_signal(pthread_cond_t *cond);int pthread_cond_broadcast(pthread_cond_t *cond); //解除所有线程的阻塞
4.清除条件变量。无线程等待,否则返回EBUSY
int pthread_cond_destroy(pthread_cond_t *cond);
接下来我们又改变一下生产者消费者问题:现在缓冲区或者仓库大小为BUFSIZE,只有一个生产者和一个消费者(其实也适用于多个生产者和消费者), 我们这个时候就可以把缓冲区设置为一个互斥量,一次要么生产者要么消费者霸占它。但接下来处理方式与互斥量有所不同:假如生产者成功占据锁(缓冲区),这时它不能马上开始往里面生产东西,要先判断缓冲区是不是满的,如果缓冲区满了,那么生产者就会把自己放到等待条件的线程列表上,然后对互斥量进行解锁,这是一个原子操作。如果缓冲区不满则可以生产产品,然后给消费者发送notempty信号,表示缓冲区有产品了, 你可以yy了。然后解锁互斥量。假如是消费者成功占据锁(缓冲区),同样它要检查缓冲区是不是空的,如果空,那么消费者就会把自己放到等待条件的线程列表上,然后对互斥量进行解锁。如果不空,消费者开始yy,然后给生产者发送nofull信号, 表示缓冲区有位置可以生产了, 你快生产吧。然后解锁互斥量。就这样,生产者消费者和谐同步工作着。
流程图我就不画了,看代码也能明白过程:
---producer过程:lock(mutex)->checknotfull->(if notfull wait until notfull)->produce product->sendnotempty to consumer->unlock(mutex)
---consumer过程:lock(mutex)->checknotempty->(if notempty wait until notempty)->get productfrom buffer->send notfull to poducer->unlock(mutex)
- #include <stdio.h>
- #include <pthread.h>
-
- #define LOOP_COUNT 20 //生产者和消费者各自循环次数,也可以说生产商品的总量
-
- #define BUFSIZE 5 //缓冲区大小,也就是最多能放多少个产品
-
- pthread_mutex_t mutex;
-
- pthread_cond_t notempty , notfull;
- unsigned int prod_pos = 3;
- unsigned int cons_pos = 0;
-
- void *producer( void *arg );
- void *consumer( void *arg );
-
- int main(int argc , char *argv[]){
- pthread_t thrd_prod , thrd_cons;
-
- pthread_mutex_init( &mutex , NULL );
-
-
- if( pthread_create( &thrd_prod , NULL, producer ,
- NULL ) != 0 )
- oops( "thread create failed." );
- sleep(1);
-
- if( pthread_create( &thrd_cons , NULL, consumer ,
- NULL ) != 0 )
- oops( "thread create failed." );
-
-
- if( pthread_join( thrd_prod , NULL ) != 0 )
- oops( " wait thread failed.");
- if( pthread_join( thrd_cons , NULL ) != 0 )
- oops( " wait thread failed.");
-
- pthread_mutex_destroy( &mutex );
- return 0;
- }
-
- void *producer( void *arg){
- int count = 0 ;
-
- while( count++ < LOOP_COUNT ){
- printf( "producer try to lock .\n");
- pthread_mutex_lock( &mutex );
-
-
-
-
- if( ( prod_pos + 1 ) % BUFSIZE == cons_pos ){
-
- printf( "producer wait not full.\n");
- pthread_cond_wait( ¬full , &mutex );
- }
-
-
- printf( "producer lock successful, producer put %d's "
- "product to buffer.\n" ,count);
- prod_pos = ( prod_pos +1 ) % BUFSIZE;
- pthread_cond_signal( ¬empty );
-
-
-
-
- sleep( 1 );
- printf("prducer finished ,unlock lock.\n");
- pthread_mutex_unlock( &mutex );
- sleep( 1 );
- }
- }
- void *consumer( void *arg ){
- int count = 0 ;
-
- while( count++ < LOOP_COUNT ){
-
- printf( "consumer try to lock .\n" );
- pthread_mutex_lock( &mutex );
-
-
-
-
- if( cons_pos == prod_pos ){
- printf( "consumer wait not empty.\n");
- pthread_cond_wait( ¬empty , &mutex );
- }
-
-
- printf( " consumer locked successful ,consumer "
- "get %d product from buffer.\n" , count);
- cons_pos = ( cons_pos + 1) % BUFSIZE ;
- pthread_cond_signal( ¬full );
-
-
-
-
-
- sleep( 1 );
- printf("consumer finished ,unlock lock.\n");
- pthread_mutex_unlock( &mutex );
- sleep(1);
- }
- }
先不忙看结果, 想想结果跟你预想的是不是一样,然后看结果:
然后排查,锁定到 pthread_cond_wait函数,查看其他资料,总结如下: 函数将解锁mutex参数指向的互斥锁,并使当前线程阻塞在cond参数指向的条件变量上。
被阻塞的线程可以被pthread_cond_signal函数,pthread_cond_broadcast函数唤醒,也可能在被信号中断后被唤醒。 pthread_cond_wait函数的返回并不意味着条件的值一定发生了变化,必须重新检查条件的值。
pthread_cond_wait函数返回时,相应的互斥锁将被当前线程锁定,即使是函数出错返回。
一般一个条件表达式都是在一个互斥锁的保护下被检查。当条件表达式未被满足时,线程将仍然阻塞在这个条件变量上。当另一个线程改变了条件的值并向条件变量发出信号时,等待在这个条件变量上的一个线程或所有线程被唤醒,接着都试图再次占有相应的互斥锁。
阻塞在条件变量上的线程被唤醒以后,直到pthread_cond_wait()函数返回之前条件的值都有可能发生变化。所以函数返回以后,在锁定相应的互斥锁之前,必须重新测试条件值。最好的测试方法是循环调用pthread_cond_wait函数,并把满足条件的表达式置为循环的终止条件。
所以上述代码应该用循环而不是if。具体修改如下:
- consumer函数中:
-
-
-
-
-
-
-
-
-
- while( cons_pos == prod_pos ){
- printf( "consumer wait not empty.\n");
-
-
-
-
-
-
-
- pthread_cond_wait( ¬empty , &mutex );
- }
- produer函数中:
-
-
- while( ( prod_pos + 1 ) % BUFSIZE == cons_pos ){
-
- printf( "producer wait not full.\n");
- pthread_cond_wait( ¬full , &mutex );
- }
这样来看结果就对了
注:关于生产者和消费者操作缓冲区的操作,大家下来仔细揣摩一下,搞懂
- while( ( prod_pos + 1 ) % BUFSIZE == cons_pos )
- while( cons_pos == prod_pos )
这两个循环条件,大家就明白缓冲区操作了。
四、信号量
关于信号量的说明见http://blog.csdn.net/big_bit/article/details/51471811,我主要写了一个程序来学习信号量,这个例子是两个相同的线程同时向屏幕输出数据,我们可以看到如何使用信号量来使两个进程协调工作,使同一时间只有一个线程可以向屏幕输出数据。
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/sem.h>
- #include <sys/ipc.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <pthread.h>
-
-
-
-
-
-
- union semun{
- int val;
- struct semid_ds *buf;
- unsigned short *array;
- };
- struct my_arg{
- char mes;
- int semid;
- unsigned short nsems;
- };
- static int creat_sem( const int nsems);
- static void del_sem( const int semid);
- static int semaphore_p( const int semid , const unsigned short nsems );
- static int semaphore_v( const int semid , const unsigned short nsems );
- static void *pr_char( void *);
-
- int main(int argc , char *argv[]){
- int sem_id;
- int nsems = 1;
-
- char mes1 = 'X';
- char mes2 = 'O';
- pthread_t thrd1,thrd2;
- struct my_arg my_arg1,my_arg2;
-
-
- sem_id = creat_sem( nsems );
-
- my_arg1.semid=my_arg2.semid = sem_id;
- my_arg1.nsems = my_arg2.nsems = (unsigned short )(nsems-1);
- my_arg1.mes = mes1;
- my_arg2.mes = mes2;
-
-
- pthread_create( &thrd1 , NULL , pr_char , (void *)&my_arg1);
- pthread_create( &thrd2 , NULL , pr_char , (void *)&my_arg2);
-
-
-
- pthread_join( thrd1 , NULL );
- pthread_join( thrd2 , NULL);
-
- if( argc > 1){
- sleep(3);
- del_sem( sem_id);
- }
- return 0;
- }
-
- static int creat_sem( const int nsems ){
- key_t key ;
- union semun arg;
- int sem_id;
-
-
-
-
-
-
- if( -1 ==( sem_id = semget( (key_t)1324 , nsems , 0666 | IPC_CREAT) ) )
- oops( "semget " );
-
-
- arg.val = nsems;
- if( -1 == semctl( sem_id , 0 , SETVAL , arg) )
- oops( " semctl " );
-
- return sem_id;
- }
-
-
- static void del_sem( const int semid ){
- union semun arg;
-
- if( -1 == semctl( semid , 0 , IPC_RMID , arg) )
- oops( "delete semaphore");
-
- }
-
- static int semaphore_p( const int semid , const unsigned short nsems ){
- struct sembuf sem_buf;
-
-
-
-
- sem_buf.sem_num = nsems;
- sem_buf.sem_op = -1 ;
- sem_buf.sem_flg = SEM_UNDO;
-
- if( -1 == semop( semid , &sem_buf , 1 ) )
- oops( " semop ");
- return 1;
- }
-
-
- static int semaphore_v( const int semid , const unsigned short nsems ){
- struct sembuf sem_buf;
-
-
-
-
- sem_buf.sem_num = nsems;
- sem_buf.sem_op = 1 ;
- sem_buf.sem_flg = SEM_UNDO;
-
- if( -1 == semop( semid , &sem_buf , 1 ) )
- oops( " semop ");
- return 1;
- }
-
- static void *pr_char(void *arg){
- static int count = 0;
- struct my_arg *parg = (struct my_arg*)arg;
- printf( " semid = %d\n" , parg->semid);
- printf( " nsems = %d\n" , parg->nsems);
- while(count++ < 10){
-
- if(!semaphore_p( parg->semid , parg->nsems))
- oops( " semaphore_p" );
- printf("%c" , parg->mes);
-
- fflush(stdout);
- sleep(rand() % 3);
-
- printf("%c" , parg->mes);
- fflush(stdout);
-
- if(!semaphore_v(parg->semid , parg->nsems))
- oops( "semaphore" );
- sleep(rand() % 2);
- }
- printf("print finished.");
- }