這個(gè)問題需要的知識(shí)主要包括:
創(chuàng)新互聯(lián)建站長期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為寬城企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站制作,寬城網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
1 多進(jìn)程間進(jìn)行通信;
2 使用同步信號(hào)量(semaphore)和互斥信號(hào)量(mutex)進(jìn)行數(shù)據(jù)保護(hù)。
參考代碼如下,可以參照注釋輔助理解:
#include?stdio.h
#include?stdlib.h
#include?unistd.h
#include?pthread.h
#include?semaphore.h
#define?N?2???//?消費(fèi)者或者生產(chǎn)者的數(shù)目
#define?M?10?//?緩沖數(shù)目
int?in?=?0;???//?生產(chǎn)者放置產(chǎn)品的位置
int?out?=?0;?//?消費(fèi)者取產(chǎn)品的位置
int?buff[M]?=?{0};?//?緩沖初始化為0,?開始時(shí)沒有產(chǎn)品
sem_t?empty_sem;?//?同步信號(hào)量,?當(dāng)滿了時(shí)阻止生產(chǎn)者放產(chǎn)品
sem_t?full_sem;???//?同步信號(hào)量,?當(dāng)沒產(chǎn)品時(shí)阻止消費(fèi)者消費(fèi)
pthread_mutex_t?mutex;?//?互斥信號(hào)量,?一次只有一個(gè)線程訪問緩沖
int?product_id?=?0;???//生產(chǎn)者id
int?prochase_id?=?0;?//消費(fèi)者id
/*?打印緩沖情況?*/
void?print()
{
int?i;
for(i?=?0;?i??M;?i++)
???printf("%d?",?buff[i]);
printf("\n");
}
/*?生產(chǎn)者方法?*/?
void?*product()
{
int?id?=?++product_id;
while(1)
{
???//?用sleep的數(shù)量可以調(diào)節(jié)生產(chǎn)和消費(fèi)的速度,便于觀察
???sleep(1);
???//sleep(1);
??
???sem_wait(empty_sem);
???pthread_mutex_lock(mutex);
??
???in?=?in?%?M;
???printf("product%d?in?%d.?like:?\t",?id,?in);
??
???buff[in]?=?1;??
???print();??
???++in;
??
???pthread_mutex_unlock(mutex);
???sem_post(full_sem);??
}
}
/*?消費(fèi)者方法?*/
void?*prochase()
{
int?id?=?++prochase_id;
while(1)
{
???//?用sleep的數(shù)量可以調(diào)節(jié)生產(chǎn)和消費(fèi)的速度,便于觀察
???sleep(1);
//sleep(1);
??
???sem_wait(full_sem);
???pthread_mutex_lock(mutex);
??
???out?=?out?%?M;
???printf("prochase%d?in?%d.?like:?\t",?id,?out);
??
???buff[out]?=?0;
???print();
???++out;
??
???pthread_mutex_unlock(mutex);
???sem_post(empty_sem);
}
}
int?main()
{
pthread_t?id1[N];
pthread_t?id2[N];
int?i;
int?ret[N];
//?初始化同步信號(hào)量
int?ini1?=?sem_init(empty_sem,?0,?M);?
int?ini2?=?sem_init(full_sem,?0,?0);??
if(ini1??ini2?!=?0)
{
???printf("sem?init?failed?\n");
???exit(1);
}?
//初始化互斥信號(hào)量?
int?ini3?=?pthread_mutex_init(mutex,?NULL);
if(ini3?!=?0)
{
???printf("mutex?init?failed?\n");
???exit(1);
}?
//?創(chuàng)建N個(gè)生產(chǎn)者線程
for(i?=?0;?i??N;?i++)
{
???ret[i]?=?pthread_create(id1[i],?NULL,?product,?(void?*)(i));
???if(ret[i]?!=?0)
???{
printf("product%d?creation?failed?\n",?i);
exit(1);
???}
}
//創(chuàng)建N個(gè)消費(fèi)者線程
for(i?=?0;?i??N;?i++)
{
???ret[i]?=?pthread_create(id2[i],?NULL,?prochase,?NULL);
???if(ret[i]?!=?0)
???{
printf("prochase%d?creation?failed?\n",?i);
exit(1);
???}
}
//銷毀線程
for(i?=?0;?i??N;?i++)
{
???pthread_join(id1[i],NULL);
???pthread_join(id2[i],NULL);
}
exit(0);?
}
在Linux下編譯的時(shí)候,要在編譯命令中加入選項(xiàng)-lpthread以包含多線程支持。比如存儲(chǔ)的C文件為demo.c,要生成的可執(zhí)行文件為demo??梢允褂妹睿?/p>
gcc demo.c -o demo -lpthread
程序中為便于觀察,使用了sleep(1);來暫停運(yùn)行,所以查看輸出的時(shí)候可以看到,輸出是每秒打印一次的。
程序代碼test.c共兩個(gè)線程,一個(gè)主線程,一個(gè)讀緩存區(qū)的線程:
#include pthread.h
#include stdio.h
#include stdlib.h
#include string.h
#include unistd.h
char globe_buffer[100];
void *read_buffer_thread(void *arg); //這里先聲明一下讀緩存的線程,具體實(shí)現(xiàn)寫在后面了
int main()
{
int res,i;
pthread_t read_thread;
for(i=0;i20;i++)
globe_buffer[i]=i;
printf("\nTest thread : write buffer finish\n");
sleep(3);\\這里的3秒是多余,可以不要。
res = pthread_create(read_thread, NULL, read_buffer_thread, NULL);
if (res != 0)
{
printf("Read Thread creat Error!");
exit(0);
}
sleep(1);
printf("waiting for read thread to finish...\n");
res = pthread_join(read_thread, NULL);
if (res != 0)
{
printf("read thread join failed!\n");
exit(0);
}
printf("read thread test OK, have fun!! exit ByeBye\n");
return 0;
}
void *read_buffer_thread(void *arg)
{
int i,x;
printf("Read buffer thread read data : \n");
for(i=0;i20;i++)
{
x=globe_buffer[i];
printf("%d ",x);
globe_buffer[i]=0;//清空
}
printf("\nread over\n");
}
---------------------------------------------------------------------------------
以上程序編譯:
gcc -D_REENTRANT test.c -o test.o –lpthread
運(yùn)行這個(gè)程序:
$ ./test.o:
內(nèi)核配置完成,輸入make命令即可開始編譯內(nèi)核。如果沒有修改Makefile文件并指定ARCH和CROSS_COMPILE參數(shù),則須在命令行中指定:
$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
目前大多數(shù)主機(jī)都是多核處理器,為了加快編譯進(jìn)度,可以開啟多線程編譯,在make的時(shí)候加上“-jN”即可,N的值為處理器核心數(shù)目的2倍。例如對(duì)于I7 4核處理器,可將N設(shè)置為8:
$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- -j8
采用多線程編譯的優(yōu)點(diǎn)是能加快編譯進(jìn)度,。具體可以參照ZLG《嵌入式Linux開發(fā)教程(下冊)》第1章。
__thread 是GCC內(nèi)置的線程局部存儲(chǔ)設(shè)施,存取效率可以和全局變量相比。
__thread 變量 每一個(gè)線程有一份獨(dú)立實(shí)體 ,各個(gè)線程的值互不干擾??梢杂脕硇揎椖切в腥中郧抑悼赡茏?,但是又不值得用全局變量保護(hù)的變量。
只能修飾 POD 類型(類似整型指針的標(biāo)量,不帶自定義的構(gòu)造、拷貝、賦值、析構(gòu)的類型,二進(jìn)制內(nèi)容可以任意復(fù)制memset,memcpy,且內(nèi)容可以復(fù)原)
??不能修飾 class 類型,因?yàn)闊o法自動(dòng)調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù),可以用于修飾全局變量,函數(shù)內(nèi)的靜態(tài)變量,不能修飾函數(shù)的局部變量或者class的普通成員變量,且__thread變量值只能初始化為編譯期常量,即編譯期間就能確定值。
場景說明:每個(gè)線程有一些需要保存的上下文信息,即可使用 __thread 變量
你編譯的時(shí)候有加多線程連接選項(xiàng)嗎? 要加上 -lpthread 或者 -pthread (盡量選后者)
例如 gcc -pthread -o test main.cpp
另外你的線程創(chuàng)建的不對(duì),函數(shù)指針不能強(qiáng)轉(zhuǎn)類型(這里也不用轉(zhuǎn))
pthread_create(producter_t,NULL,(void*)producter_f,NULL);
pthread_create(consumer_t,NULL,(void*)consumer_f,NULL);
應(yīng)該是
pthread_create(producter_t,NULL,producter_f,NULL);
pthread_create(consumer_t,NULL,consumer_f,NULL);
gcc xxx.c -lpthread 其中的-l是指包含的lib庫,具體寫法可以man gcc看下
多線程函數(shù)除了要包含頭文件pthread.h外還必須要包含lib庫pthread
pthread_create是創(chuàng)建線程,但具體的線程里面做什么事是在void *create(void *arg)里,這個(gè)函數(shù)名是自己任意區(qū)的,但返回值和參數(shù)一般都是void*類型,因?yàn)閜thread_create函數(shù)的定義就是這樣
int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);
網(wǎng)站欄目:linux多線程編譯命令 linux shell 多線程執(zhí)行程序
URL分享:http://muchs.cn/article2/docsooc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、小程序開發(fā)、手機(jī)網(wǎng)站建設(shè)、虛擬主機(jī)、微信小程序、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容