本文可任意转载,但必须注明作者和出处。
【原创】手把手教你Linux下的多线程设计(四)
--Linux下多线程编程详解
原创作者:Frozen_socker(冰棍)
线程同步
首先来看一下有关同步机制的概念。同步就是若干个线程等待某个事件的发生,当等待事件发生时,一起开始继续执行。可以这样简单理解同步,就是若干个线程各自对自己的数据进行处理,然后在某个点必须汇总一下数据,否则不能进行下一步的处理工作。
线程同步的函数调用有pthread_cond_init、pthread_cond_broadcast、pthread_cond_signal、pthread_cond_wait和pthread_cond_destroy
函数原型:
int pthread_cond_init(pthread_cond_t *
restrict cond,
const pthread_condattr_t *restrict attr);
函数说明:
按attr指定的属性初始化cond条件变量。如果attr为NULL,效果等同于pthread_cond_t cond = PTHREAD_COND_INITIALIZER
函数原型:
int pthread_cond_broadcast(pthread_cond_t *cond);
函数说明:
对所有等待cond这个条件变量的线程解除阻塞。
函数原型:
int pthread_cond_signal(pthread_cond_t *cond);
函数说明:
仅仅解除等待cond这个条件变量的某一个线程的阻塞状态。如果有若干线程挂起等待该条件变量,该调用只唤起一个线程,被唤起的线程是哪一个是不确定的。
函数原型:
int pthread_cond_wait(pthread_cond_t *
restrict cond,
pthread_mutex_t *restrict mutex);
函数说明:
该调用自动阻塞发出调用的当前线程,并等待由参数cond指定的条件变量,而且为参数mutex指定的互斥体解锁。被阻塞线程直到有其他线程调用pthread_cond_signal或pthread_cond_broadcast函数置相应的条件变量时,而且获得mutex互斥体才解除阻塞。等待状态下的线程不占用CPU时间。
函数原型:
int pthread_cond_timedwait(pthread_cond_t *
restrict cond,
pthread_mutex_t *
restrict mutex,
const struct timespec *restrict abstime);
函数说明:
该函数自动阻塞当前线程等待参数cond指定的条件变量,并为参数mutex指定的互斥体解锁。被阻塞的线程被唤起继续执行的条件是:有其他线程对条件变量cond调用pthread_cond_signal函数;或有其他线程对条件变量cond调用pthread_cond_broadcast;或系统时间到达abstime参数指定的时间;除了前面三个条件中要有一个被满足外,还要求该线程获得参数mutex指定的互斥体。
函数原型:
int pthread_cond_destroy(pthread_cond_t *cond);
函数说明:
释放cond条件变量占用的资源。
看下面的示例:
//example_5.c
#include <stdio.h>
#include
<pthread.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
pthread_t pt1,pt2;
pthread_mutex_t mu;
pthread_cond_t cond;
int i = 1
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void * decrease(void *
arg)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
while(1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
pthread_mutex_lock(&mu);
if(++i)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
printf("%d ",i);
if(i != 1) printf("Error ");
pthread_cond_broadcast(&cond);
pthread_cond_wait(&cond,&mu);
}
sleep(1);
pthread_mutex_unlock(&mu);
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void * increase(void *
arg)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
while(1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
pthread_mutex_lock(&mu);
if(i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
printf("%d ",i);
if(i != 0) printf("Error ");
pthread_cond_broadcast(&cond);
pthread_cond_wait(&cond,&mu);
}
sleep(1);
pthread_mutex_unlock(&mu);
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int
main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
pthread_create(&pt2,NULL,increase,NULL);
pthread_create(&pt1,NULL,decrease,NULL);
pthread_join(pt1,NULL);
pthread_join(pt2,NULL);
}
以上我们讲解过了Linux下利用pthread.h头文件的多线程编程知识,下一章中,我们继续讲解关于多线程的深入编程知识,敬请期待。