Intro
- 本文为多生产者多消费者的C语言实现
- 生产者与消费者针对链表的头结点进行操作
- 使用到的线程同步方法为互斥锁+条件变量
- 本文侧重于理解条件变量的使用
- 为保证代码简洁,本文代码没有对函数的返回值进行检查,聪明的你一定知道如何检查这个小细节(•̀ᴗ•́)و ̑̑
show me the code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
struct Node{
int number;
struct Node* next;
};
pthread_mutex_t mutex;
pthread_cond_t cond;
struct Node *head = NULL;
void *producer(void *arg);
void *consumer(void *arg);
void Init(pthread_t *ptid, pthread_t *ctid);
void Dest(pthread_t *ptid, pthread_t *ctid);
int main(int argc, char **argv)