Linux:线程

复习上节课:
1.线程安全:多线程程序,无论调度顺序怎么样,都会得到正确的一致的结果
线程安全的函数(可重入函数)
线程实现:用户,内核,组合
linux线程实现:内核实现
2.线程同步方法:信号量 互斥锁 条件变量 读写锁
全局变量和互斥锁同时使用
3.线程函数(上锁,解锁)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>

pthread_mutex_t mutex;//上锁
pthread_cond_t cond;//条件变量

void* fun1(void* arg)
{
    
    
    char* s=(char*)arg;
    if(s==NULL)
    {
    
    
        pthread_exit(NULL);
    }
    while(1)
    {
    
    
        pthread_mutex_lock(&mutex);//加锁操作
        pthread_cond wait(&cond,&mutex);//阻塞&unlock
        pthread_mutex_unlock(&mutex);//解锁
        
        if(strncmp(s,"end",3)==0)
        {
    
    
        	break;
        }
        printf("fun1 read:%s\n",s);
    }
}
void* fun2(void* arg)
{
    
    
    char* s=(char*)arg;
    if(s==NULL)
    {
    
    
        pthread_exit(NULL);
    }
    while(1)
    {
    
    
        pthread_mutex_lock(&mutex);//加锁操作
        pthread_cond wait(&cond,&mutex);
        pthread_mutex_unlock(&mutex);//解锁
        
        if(strncmp(s,"end",3)==0)
        {
    
    
        	break;
        }
        printf("fun2 read:%s\n",s);
    }
}
int main()
{
    
    
	//初始化
	pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);
    
    char buff[128]={
    
    0};
    pthread_t id1,id2;
    //创建线程
    pthread_create(&id1,NULL,fun1,NULL);
    pthread_create(&id2,NULL,fun2,NULL);
    
    while(1)
    {
    
    
        fgets(buff,128,stdin);
        
        if(strncmp(buff,"end",3)==0)
        {
    
    
        	pthread_mutex_lock(&mutex);
        	pthread_cond_broadcast(&cond);//唤醒所有程序为了所有都能正常退出
        	pthread_mutex_unlock(&mutex);
        	break;
        }
        else
        {
    
    
        	pthread_mutex_lock(&mutex);
        	pthread_cond_signal(&cond);
        	pthread_mutex_unlock(&mutex);
        }
    }
    pthread_join(id1,NULL);
    pthread_join(id2,NULL);
    
    pthread_cond_destroy(&cond);
    pthread_mutex_destroy(&mutex);
	exit(0);
;}

运行结果:
在这里插入图片描述
进等待队列,出等待队列和唤醒时候都需要加锁,互斥地程序都需要加锁
读写锁:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>

pthread_rwlock_t rwlock;//读写锁
void* fun1(void* arg)//读锁
{
    
    
	while(1)
	{
    
    
		pthread_rwlock_rdlock(&rwlock);
		printf("fun1 read...start\n");
		sleep(1);
		printf("fun1 read...end\n");
		pthread_rwlock_unlock(&rwlock);
		int n=rand()%3;
		sleep(n);
	}
}
void* fun2(void* arg)//读锁
{
    
    
	while(1)
	{
    
    
		pthread_rwlock_rdlock(&rwlock);
		printf("fun2 read...start\n");
		sleep(2);
		printf("fun2 read...end\n");
		pthread_rwlock_unlock(&rwlock);
		int n=rand()%3;
		sleep(n);
	}
}
void* fun3(void* arg)//写锁
{
    
    
	while(1)
	{
    
    
		pthread_rwlock_wrlock(&rwlock);
		printf("fun3 write start\n");
		sleep(2);
		printf("fun3 write end\n");
		int n=rand()%3;
		sleep(n);
	}
}
int main()
{
    
    
	//读写锁初始化,创建三个线程
	pthread_rwlock_init(&rwlock,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(&rwlock);
}

运行结果;
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_48580892/article/details/120617318