栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux:线程

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux:线程

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

#include
#include
#include
#include
#include

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:%sn",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:%sn",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
#include
#include
#include
#include

pthread_rwlock_t rwlock;//读写锁
void* fun1(void* arg)//读锁
{
	while(1)
	{
		pthread_rwlock_rdlock(&rwlock);
		printf("fun1 read...startn");
		sleep(1);
		printf("fun1 read...endn");
		pthread_rwlock_unlock(&rwlock);
		int n=rand()%3;
		sleep(n);
	}
}
void* fun2(void* arg)//读锁
{
	while(1)
	{
		pthread_rwlock_rdlock(&rwlock);
		printf("fun2 read...startn");
		sleep(2);
		printf("fun2 read...endn");
		pthread_rwlock_unlock(&rwlock);
		int n=rand()%3;
		sleep(n);
	}
}
void* fun3(void* arg)//写锁
{
	while(1)
	{
		pthread_rwlock_wrlock(&rwlock);
		printf("fun3 write startn");
		sleep(2);
		printf("fun3 write endn");
		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);
}

运行结果;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/297502.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号