一、思维导图
二、使用互斥锁或者信号量
实现一个简单的生产者消费者模型 一个线程每秒生产3个苹果,另一个线程每秒消费8个苹果
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int apple = 0;
pthread_mutex_t Per; //生产者
pthread_mutex_t Cer; //消费者
void *run(void *arg){
while(1){
pthread_mutex_lock(&Cer);
apple -= 8;
printf("消费8个苹果,现有%d个苹果\n", apple);
sleep(1);
pthread_mutex_unlock(&Per);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&Per, 0);
pthread_mutex_init(&Cer, 0);
pthread_mutex_lock(&Cer);
pthread_t id;
pthread_create(&id, 0, run, 0);
pthread_detach(id);
while(1){
pthread_mutex_lock(&Per);
apple += 3;
printf("生产3个苹果,现有%d个苹果\n", apple);
sleep(1);
pthread_mutex_unlock(&Per);
if(apple >= 8){
pthread_mutex_unlock(&Cer);
sleep(1);
}
}
return 0;
}
三、有一个盘子
盘子里面最多放3个苹果,5个橘子 2个生产者线程,一个每秒放1个苹果,另一个每秒2个橘子 放了苹果就不能放橘子,放了橘子就不能放苹果 2个消费者线程,1号消费者线程每秒消费2个苹果,2号消费者线程,每秒消费3个橘子
#include <myhead.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int apple = 0;
int orange = 0;
pthread_mutex_t m1;
pthread_mutex_t m2;
pthread_cond_t c1;
pthread_cond_t c2;
void *run1(void *arg){
while(1){
pthread_mutex_lock(&m1);
pthread_cond_wait(&c1, &m1);
apple -= 2;
printf("1号消费2个苹果,现有:苹果(%d),橘子(%d)\n", apple, orange);
pthread_mutex_unlock(&m1);
sleep(1);
}
}
void *run2(void *arg){
while(1){
pthread_mutex_lock(&m2);
pthread_cond_wait(&c2, &m2);
orange -= 3;
printf("1号消费3个橘子,现有:苹果(%d),橘子(%d)\n", apple, orange);
pthread_mutex_unlock(&m2);
sleep(1);
}
}
int main(int argc, const char *argv[])
{
srand(time(0));
pthread_mutex_init(&m1, 0);
pthread_cond_init(&c1, 0);
pthread_mutex_init(&m2, 0);
pthread_cond_init(&c2, 0);
pthread_t id1, id2;
pthread_create(&id1, 0, run1, 0);
pthread_create(&id2, 0, run2, 0);
pthread_detach(id1);
pthread_detach(id2);
while(1){
int i = rand() % 2 + 1;
if(i == 1 && apple < 3){
pthread_mutex_lock(&m1);
apple += 1;
printf("生产1个苹果,现有:苹果(%d),橘子(%d)\n", apple, orange);
if(apple >= 2){
pthread_cond_signal(&c1);
}
if(orange >= 3){
pthread_cond_signal(&c2);
}
sleep(1);
pthread_mutex_unlock(&m1);
}else if(i == 2 && orange < 4){
pthread_mutex_lock(&m2);
orange += 2;
printf("生产2个橘子,现有:苹果(%d),橘子(%d)\n", apple, orange);
if(apple >= 2){
pthread_cond_signal(&c1);
}
if(orange >= 3){
pthread_cond_signal(&c2);
}
sleep(1);
pthread_mutex_unlock(&m2);
}
}
return 0;
}
四、封装静态库
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <assert.h>
int getch(){
int c=0;
struct termios org_opts, new_opts;
int res=0;
res=tcgetattr(STDIN_FILENO, &org_opts);
assert(res==0);
new_opts = org_opts;
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
c=getchar();
res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res==0);
return c;
将上述函数封装成静态库,并测试,描述该函数的功能
}
封装:
gcc -c getch.c -o getch.o
ar cvr libgetch.a -o getch.o
测试:
gcc test.c -lgetch
//test.c
#include <stdio.h>
int getch();
int main(int argc, const char *argv[])
{
printf("请输入一个数:\n");
int num = getch();
printf("你输入的是:%c\n", num);
return 0;
}