mutex =PTHREAD_MUTEX_INITIALIZER;
//!> 初始化互斥锁
pthread_cond_t
cond =PTHREAD_COND_INITIALIZER;
//!> 初始化条件变量
int
g_i =0;
//!> 线程1入口
void * entrance_1( void * arg )
{
for( g_i =1; g_i < 10; g_i++ )
{
pthread_mutex_lock( &mutex);
//!> 上锁
if( g_i % 3== 0 )
//!> 给一个唤醒条件
{
pthread_cond_signal( &cond);
//!> 执行唤醒
}
else
{
printf("线程1在执行:%d\n", g_i);
}
pthread_mutex_unlock( &mutex);
//!> 解锁
sleep( 1);
}
}
//!> 线程2入口
void* entrance_2( void * arg )
{
while( g_i< 10 )
{
pthread_mutex_lock( &mutex);
//!> 加锁
if( g_i % 3!= 0 )
{
pthread_cond_wait( &cond, &mutex);
//!> 等待
}
printf("线程2在执行:%d\n", g_i);
pthread_mutex_unlock( &mutex);
//!> 解锁
sleep( 1);
}
}
int main( )
{
pthread_t
tid1;
//!> 线程1
pthread_t
tid2;
//!> 线程2
if(pthread_create( &tid1, NULL, entrance_1, NULL ) !=0 )
{
printf("创建线程1失败....\n");
exit(EXIT_FAILURE );
}
if(pthread_create( &tid2, NULL, entrance_2, NULL ) !=0 )
{
printf("创建线程2失败....\n");
exit(EXIT_FAILURE );
}
pthread_join( tid1, NULL);
//!> 等待...
pthread_join( tid2, NULL);
//!> 等待...
pthread_mutex_destroy( &mutex);
//!> 销毁
pthread_cond_destroy( &cond);
//!> 销毁
return0;
}
结果:
线程1在执行:1
线程1在执行:2
线程2在执行:3
//!> ------> 222
线程1在执行:4
线程1在执行:5
线程2在执行:6
//!>------>222
线程1在执行:7
线程1在执行:8
线程2在执行:9
//!> ------>222
2.生产消费者
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define
MAX
5
pthread_mutex_t
mutex =PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t
cond =PTHREAD_COND_INITIALIZER;
typedef struct
{
char
buf[MAX];
int
count;
}BUFFER;
BUFFER share = { "", 0};
//!> 初始化
char ch = 'A';
void * _read_( void * arg )
{
int n =0;
printf("读:%d
开始....\n",(unsigned)pthread_self());
while( ch !='Z' )
{
pthread_mutex_lock( &mutex);
//!> 加锁
if(share.count != MAX)
//!> 不是最大就读,就是一次性读入
{
share.buf[share.count++] =ch++;
//!> 读入
printf("读%d:当前字符 = %c\n", (unsigned)pthread_self(), ch-1);
if(share.count == MAX )//!> 达到MAX,不读入
{
printf("读%d:缓冲区满!\n", (unsigned)pthread_self());
pthread_cond_signal( &cond);
//!> ??????
}
}
pthread_mutex_unlock(&mutex);
//!> 解锁互斥量
}
sleep( 1);
printf("读%d:退出咯~\n", (unsigned)pthread_self());
}
void * _write_( void * arg )
{
int i, n =0;
printf("写:%d
开始....\n",(unsigned)pthread_self());
while( ch !='Z' )
{
pthread_mutex_lock( &mutex );
printf("写%d:等待...\n", (unsigned)pthread_self());
while(share.count != MAX )
{
pthread_cond_wait( &cond, &mutex);
//!> 只要buf不满等待...
}
printf("写%d:开始写入...\n", (unsigned)pthread_self());
for( i = 0;share.count > 0 ; i++, share.count-- )
{
printf("%c
",share.buf[i]);
//!> 输出看看...
if(share.count == 1 )
{
printf("\n");
//!> 换行格式而已
}
}
pthread_mutex_unlock( &mutex );
}
sleep( 1);
printf("写%d:退出...\n", (unsigned)pthread_self());
}
int main()
{
pthread_t
tid_read,tid_write;
if (pthread_create( &tid_read, NULL, _read_, NULL ) !=0 )
{
printf("创建线程1失败...\n");
exit(EXIT_FAILURE );
}
if (pthread_create( &tid_write, NULL, _write_, NULL )!= 0 )
{
printf("创建线程2失败...\n");
exit(EXIT_FAILURE );
}
pthread_join( tid_read, NULL );
pthread_join( tid_write, NULL );
pthread_mutex_destroy( &mutex );
pthread_cond_destroy( &cond );
return0;
}
/*
读:1680848640
开始....
读1680848640:当前字符 = A
读1680848640:当前字符 = B
读1680848640:当前字符 = C
读1680848640:当前字符 = D
读1680848640:当前字符 = E
读1680848640:缓冲区满!
写:1672455936
开始....
写1672455936:等待...
写1672455936:开始写入...
A
B
C
D
E
写1672455936:等待...
读1680848640:当前字符 = F
读1680848640:当前字符 = G
读1680848640:当前字符 = H
读1680848640:当前字符 = I
读1680848640:当前字符 = J
读1680848640:缓冲区满!
写1672455936:开始写入...
F
G
H
I
J
写1672455936:等待...
读1680848640:当前字符 = K
读1680848640:当前字符 = L
读1680848640:当前字符 = M
读1680848640:当前字符 = N
读1680848640:当前字符 = O
读1680848640:缓冲区满!
写1672455936:开始写入...
K
L
M
N
O
写1672455936:等待...
读1680848640:当前字符 = P
读1680848640:当前字符 = Q
读1680848640:当前字符 = R
读1680848640:当前字符 = S
读1680848640:当前字符 = T
读1680848640:缓冲区满!
写1672455936:开始写入...
P
Q
R
S
T
写1672455936:等待...
读1680848640:当前字符 = U
读1680848640:当前字符 = V
读1680848640:当前字符 = W
读1680848640:当前字符 = X
读1680848640:当前字符 = Y
读1680848640:缓冲区满!
写1672455936:开始写入...
U
V
W
X
Y
写1672455936:退出...
读1680848640:退出咯~
//******************************************************************************************************************************************************
1. 条件变量:
条件变量是用来等待而不是用来上锁的。条件变量用来自动阻塞一个线程,直到
某特殊情况发生为止。通常条件变量和互斥锁同时使用。
条件变量使我们可以睡眠等待某种条件出现。
条件变量是利用线程间共享的全局变量进行同步的机制,主要包括两个动作:
>: 一个线程等待"条件变量的条件成立"而挂起;
>: 另一个线程使"条件成立"(给出条件成立信号)。可能是一个进程的输出是另一
个的输入呗~
注意:
条件的检测是在互斥锁的保护下进行的。如果一个条件为假,一个线程自动阻
塞,并释放等待状态改变的互斥锁。如果另一个线程改变了条件,它发信号给关
联的条件变量,唤醒一个或多个等待它的线程,重新获得互斥锁,重新评价条
件。如果两进程共享可读写的内存,条件变量可以被用来实现这两进程间的线程
同步。
和互斥变量一样,使用条件变量之前要先进行初始化,也有2方法:
1.静态:pthead_cond_t
condition =PTHREAD_COND_INITIALIZER
2.动态:pthread_cond_init
2. 条件变量初始化函数
pthread_cond_init
#include<pthread.h>
intpthread_cond_init( thread_cond_t * cond, pthread_condattr_t * attr);
参数:
cond
条件变量
attr
条件变量属性
成功返回0,出错返回错误编号。
注意:如果参数attr为空,那么它将使用缺省的属性来设置所指定的条件变量。
3. 条件变量摧毁函数
#include<pthread.h>
intpthread_cond_destroy( pthread_cond_t * cond );
成功返回0,出错返回错误编号。
注意:摧毁所指定的条件变量,同时将会释放所给它分配的资源。调用该函数的
进程也并不要求等待在参数所指定的条件变量上。
4. 条件变量等待函数
#include<pthread.h>
intpthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t*mutex);
intpthread_cond_timedwait(pthread_cond_t *cond,pthread_mutex_t *
mytex,conststruct timespec *abstime);
参数:
cond条件变量
mutex互斥锁
注意区别:
函数pthread_cond_timedwait函数类型与函数pthread_cond_wait,区别在
于,如果达到或是超过所引用的参数*abstime,它将结束并返回错误
ETIME.
typedefstruct timespec
{
time_t
tv_sec;
//!> 秒
long
tv_nsex;
//!> 毫秒
}timespec_t;
也就是说当时间超过预设值后就返回错误!
5. 条件变量通知函数
#include<pthread.h>
intpthread_cond_signal( pthread_cond_t * cond );
intpthread_cond_broadcast( pthread_cond_t * cond );
参数:cond
条件变量
成功返回0,出错返回错误编号。
pthread_cond_signal:只唤醒一个线程
pthread_cond_broadcast:唤醒所有线程
注意:
>: 当调用pthread_cond_signal时一个在相同条件变量上阻塞的线程将被解锁。
>: 如果同时有多个线程阻塞,则由调度策略确定接收通知的线程。
>:如果调用pthread_cond_broadcast,则将通知阻塞在这个条件变量上的所有线
程。一旦被唤醒,线程仍然会要求互斥锁。
>: 如果当前没有线程等待通知,则上面两种调用实际上成为一个空操作。如果参
数*cond指向非法地址,则返回值EINVAL。
>::::: 我们有code可以知道,其实本质还是等待,只不过是为了某个条件而等待、、、
5. 条件变量属性讨论
其实和我们之前说的“互斥量的属性”基本框架是一样的。。。
下面就是讨论函数。。。
6. 初始化属性函数:
#include<pthread.h>
intpthread_condattr_init( pthread_condattr * attr);
若成功返回0,若失败返回错误编号。
初始化的是默认的属性、、、、、
7. 销毁函数:
#include<pthread.h>
intpthread_condattr_destroy( pthread_condattr_t * attr );
若成功返回0,若失败返回错误编号。
8. 查看属性函数:
#include<pthread.h>
intpthread_condattr_getshared( const pthread_condattr_t * attr, int *shared );
若成功返回0,若失败返回错误编号。
9. 修改属性参数:
#include<pthread.h>
intpthread_condattr_setshared( const pthread_condattr_t * attr, intshared );
若成功返回0,若失败返回错误编号。
关于shared的取值:
PTHREAD_PROCESS_PRIVATE(一个进程内使用)
PTHREAD_PROCESS_SHARED(进程间共享).