一.生产者消费者(信号量与互斥锁应用)
1.问题概述
2.图示:
3.代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFF_MAX 30//假设缓冲区的容量为30
#define SC_NUM 2//生产者2个
#define XF_NUM 3//消费者3个
int arr[BUFF_MAX];//存放的数据
int in = 0;//生产者消耗缓冲区的数量
int out = 0;//消费者释放缓冲区的数量
sem_t sem_empty;//定义空
sem_t sem_full;//定义满
pthread_mutex_t mutex;//定义互斥锁
void* sc_fun(void* arg)//生产者
{
for(int i = 0; i < 30; i++ )//两个生产者每人插入30个数据,一共60个
{
sem_wait(&sem_empty);//ps1
pthread_mutex_lock(&mutex);
arr[in] = rand() % 100;
printf("生产者在%d位置,产生数据%d\n",in,arr[in]);
in = (in + 1) % 30;//当生产的数据放满缓冲区时,将其置为0,就可以继续放数据
pthread_mutex_unlock(&mutex);
sem_post(&sem_full);//vs2
}
}
void * xf_fun(void* arg)//消费者
{
for(int i = 0; i < 20; i++ )//三个消费者每人插入20个数据,一共60个,刚好消耗完生产者生产的所有数据
{
sem_wait(&sem_full);//ps2
pthread_mutex_lock(&mutex);
printf("-----消费者在%d位置,取数据:%d\n",out,arr[out]);
out = (out + 1) % 30;
pthread_mutex_unlock(&mutex);
sem_post(&sem_empty);//vs1
}
}
int main()
{
sem_init(&sem_empty,0,30);
sem_init(&sem_full,0,0);
pthread_mutex_init(&mutex,NULL);
pthread_t sc_id[SC_NUM];
for(int i = 0; i < SC_NUM; i++ )
{
pthread_create(&sc_id[i],NULL,sc_fun,NULL);
}
pthread_t xf_id[XF_NUM];
for(int i = 0; i < XF_NUM; i++ )
{
pthread_create(&xf_id[i],NULL,xf_fun,NULL);
}
for(int i = 0; i < SC_NUM; i++ )
{
pthread_join(sc_id[i],NULL);
}
for(int i = 0; i < XF_NUM; i++ )
{
pthread_join(xf_id[i],NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&sem_empty);
sem_destroy(&sem_full);
exit(0);
}
二.读写锁(读多写少的场景,读操作可以一块读,写操作只能一个一个写)
1.分为读锁与写锁,可以自行分配,只允许多个读锁可以共同进行,读锁与写锁,写锁与写锁不能同时进行。
2. pthread_rwlock_init(pthread_rwlock*restrict rwlock(读写锁地址),const pthread_rwlockattr_t*restrict attr(一般为NULL))-->读写锁初始化
pthread_rwlock_destroy(pthread_rwlock_t*rwlock(读写锁地址))-->销毁读写锁
pthread_rwlock_rdlock(pthread_rwlock_t*rwlock(读写锁地址))-->加读锁
pthread_rwlock_wrlock(pthread_rwlock_t*rwlock(读写锁地址))-->加写锁
pthread_rwlock_unlock(pthread_rwlock_t*rwlock(读写锁地址))-->解锁
3.示例:创建两个线程模拟读操作(可以同时进行),再创建一个写操作(两个读可以同时进行,但读写不能同时进行)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
pthread_rwlock_t lock;
void*fun1(void*arg)//读操作
{
for(int i=0;i<10;i++)
{
pthread_rwlock_rdlock(&lock);//读加锁
printf("fun1 start\n");
int n=rand()%3;
sleep(n);
printf("fun1 end\n");
pthread_rwlock_unlock(&lock);//写锁
sleep(n);
}
}
void*fun1(void*arg)//读操作
{
for(int i=0;i<10;i++)
{
pthread_rwlock_rdlock(&lock);//读加锁
printf("fun2 start\n");
int n=rand()%3;
sleep(n);
printf("fun2 end\n");
pthread_rwlock_unlock(&lock);//写锁
sleep(n);
}
}
void*fun1(void*arg)//写操作
{
for(int i=0;i<10;i++)
{
pthread_rwlock_wrlock(&lock);//写加锁
printf("******fun3 start\n");
int n=rand()%3;
sleep(n);
printf("******fun3 end\n");
pthread_rwlock_unlock(&lock);//写锁
sleep(n);
}
}
int main()
{
pthread_rwlock_init(&lock,NULL);//初始化读写锁
pthread_t id1,id2,id3;
pthread_create(&id1,NULL,fun1,NULL);//创建线程
pthread_create(&id2,NULL,fun2,NULL);
pthread_create(&id3,NULL,fun3,NULL);
pthread_join(id1,NULL);//等待线程结束
pthread_join(id2,NULL);
pthread_join(id3,NULL);
pthread_rwlock_destroy(&lock);
exit(0);
}
三.线程安全
1.多线程程序不管怎么运行都不会出现结果错误就是线程安全(多线程程序无论调度的顺序如何,都能得到正确的结果)。解决方法:线程同步(信号量,互斥锁,读写锁,条件变量),使用线程安全的函数等等
2.使用线程安全的函数:下面的示例关于函数char * strtok(char*str(字符数组),const char*delim(分割符))(分割一个字符数组)-->a/bbb///cc-->分割为 a bbb cc
在两个线程进行strtok函数的使用:若使用strtok则会使两个buff产生混乱,可能会打印对方应该打印的值,造成错误。所以应该使用线程安全的函数strtok_r(char*str(字符数组),const char*delim(分割符),char** saveptr())
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
void* fun(void* arg)
{
char buff[] = "a b c d e f";//buff各是各的,在各自的栈上定义的
char* ptr = NULL;//用这个指针记住当前buff的位置
char* s = strtok_r(buff," ",&ptr);//分割符为空格
while( s != NULL )
{
printf("fun s=%s\n",s);
sleep(1);
s = strtok_r(NULL," ",&ptr);
}
}
int main()
{
pthread_t id;
pthread_create(&id,NULL,fun,NULL);
char buff[] = "1 2 3 4 5 6";//buff各是各的,在各自的栈上定义的
char* ptr = NULL;//用这个指针记住当前buff的位置
char* s = strtok_r(buff," ",&ptr);//分割符为空格
while( s != NULL )
{
printf("main s=%s\n",s);
sleep(1);
s = strtok_r(NULL," ",&ptr);//传NULL,就是沿着刚才分割完的位置继续分割
}
pthread_join(id,NULL);
exit(0);
}
3.线程的实现:一共三种方式:
用户级线程(在用户空间,用线程库实现线程的创建)-->可以创建很多个线程,线程的创建管理都由线程库中的代码完成,创建开销小,缺点是不能使用多个处理器,内核会当成一个线程处理,只能够进行并发运行(交替执行)。
内核级线程(linux)-->内核参与了线程的创建,线程的创建管理都由内核完成,创建开销大。好处是可以运用多个处理器,进行并行。
组合级线程-->有以上两种