1.条件变量的基本作用
//!> 条件变量的基本作用
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//!> 注意此处为了方便就使用静态变量吧
pthread_mutex_t
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:退出咯~
//!> 条件变量的基本作用
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//!> 注意此处为了方便就使用静态变量吧
pthread_mutex_t
pthread_cond_t
int
//!> 线程1入口
void * entrance_1( void * arg )
{
}
//!> 线程2入口
void* entrance_2( void * arg )
{
}
int main( )
{
}
2.生产消费者
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define
pthread_mutex_t
pthread_cond_t
typedef struct
{
}BUFFER;
BUFFER share = { "", 0};
char ch = 'A';
void * _read_( void * arg )
{
}
void * _write_( void * arg )
{
}
int main()
{
}
/*