#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
int h= 0;
// 线程同步 - 互斥锁
pthread_mutex_t mutex;
// 阻塞线程 - 条件变量类型的变量
pthread_cond_t cond;
// 生产者
void* producer(void* arg)
{
while(1)
{
// 使用互斥锁保护共享数据
pthread_mutex_lock(&mutex);
h = 8;
printf("====== produce: %d\n",h);
pthread_mutex_unlock(&mutex);
// 通知阻塞的消费者线程,解除阻塞
pthread_cond_signal(&cond);
sleep(rand() % 3);
}
return NULL;
}
void* customer(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
// 判断链表是否为空
if(h == 0)
{
// 线程阻塞
// 该函数会对互斥锁解锁
pthread_cond_wait(&cond, &mutex);
// 解除阻塞之后,对互斥锁做加锁操作
}
h=0;
printf("------ customer: %d\n",h);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main(int argc, const char* argv[])
{
pthread_t p1, p2;
// init
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
// 创建生产者线程
pthread_create(&p1, NULL, producer, NULL);
// 创建消费者线程
pthread_create(&p2, NULL, customer, NULL);
// 阻塞回收子线程
pthread_join(p1, NULL);
pthread_join(p2, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}