<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > Linux多線(xiàn)程同步方法

Linux多線(xiàn)程同步方法

作者: 時(shí)間:2011-08-11 來(lái)源:網(wǎng)絡(luò ) 收藏

以下是的幾種方式:

本文引用地址:http://dyxdggzs.com/article/150368.htm

1、 互斥量。

通過(guò)使用pthread的互斥接口保護數據,確保同一時(shí)間只有一個(gè)訪(fǎng)問(wèn)數據?;コ饬繌谋举|(zhì)上講是一把鎖,在訪(fǎng)問(wèn)共享資源前對互斥量進(jìn)行加鎖,在訪(fǎng)問(wèn)完成后釋放互斥量上的鎖。如下例所示,就是互斥量對共享數據的操作:

#include stdio.h>
#include pthread.h>
int value = 5;//共享變量
pthread_mutex_t mutex;//互斥變量
void *mythread1();
void mainshow();
int main()
{
int retval;
pthread_t tid1;
retval = pthread_create(tid1,NULL,mythread1,value);//創(chuàng )建
if(retval != 0){printf(“Can not create mythread1n”);
mainshow();
retval = pthread_join(tid1,NULL);//等待線(xiàn)程mythread1結束
if(retval != 0){printf(“Can not join with mythread.n”);
printf(“value = %dn”,value);
return 0;
}

void *mythread1()
{
int retval;
retval = pthread_mutex_lock(mutex);//上鎖
value = value + 1;//對共享變量的操作
printf(value = %dn,value);
retval = pthread_mutex_unlock(mutex);//解鎖
pthread_exit((void *)0);
}


void myshow()
{
int retval;
retval = pthread_mutex_lock(mutex);//上鎖
value = value + 1;//對共享變量的操作
printf(“value = %dn”,value);
pthread_mutex_unlock(mutex);//解鎖
}

2、信號量

該信號量是Posix提供的基于內存的信號量,它們由應用程序分配信號量的內存空間。如下例所示,就是信號量對共享數據的操作:

#include stdio.h>
#include pthread.h>
#include semaphore.h>
int value = 5;
sem_t sem1,sem2;
void mainshow();
void *mythread();
int main()
{
int retval;
pthread_t tid;
retval = sem_init(sem1,0,0);
retval = sem_init(sem2,0,1);
retval =pthread_create(tid,NULL,mythread,NULL);
mainshow();
pthread_join(tid,NULL);


printf(value3 = %dn,value);
return 0;
}


void *mythread()
{
int retval;
retval = sem_wait(sem1);
value = value + 1;
printf(value1 = %dn,value);
retval = sem_post(sem2);
pthread_exit((void *) 0);
}


void mainshow()
{
int retval;
retval = sem_wait(sem2);
value = value + 1;
printf(value2 = %dn,value);
retval = sem_post(sem1);
}

linux操作系統文章專(zhuān)題:linux操作系統詳解(linux不再難懂)

linux相關(guān)文章:linux教程




關(guān)鍵詞: 方法 同步 線(xiàn)程 Linux

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>