www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]?????? 我想做的就是對(duì)每個(gè)線程進(jìn)行有針對(duì)性的控制,也即可以對(duì)線程進(jìn)行暫停,恢復(fù),退出等等精細(xì)控制,對(duì)于此項(xiàng)要求,我的想法是聲明一個(gè)類,該類中有些精細(xì)的操作其中包括該線程的狀態(tài),對(duì)線程控制的互斥變

?????? 我想做的就是對(duì)每個(gè)線程進(jìn)行有針對(duì)性的控制,也即可以對(duì)線程進(jìn)行暫停,恢復(fù),退出等等精細(xì)控制,對(duì)于此項(xiàng)要求,我的想法是聲明一個(gè)類,該類中有些精細(xì)的操作其中包括該線程的狀態(tài),對(duì)線程控制的互斥變量(pthread_mutex_t),以及喚醒的條件(pthread_cond_t),要實(shí)現(xiàn)這些功能其核心在于,每個(gè)線程擁有自己的控制變量,也即線程執(zhí)行函數(shù)是線程類的靜態(tài)函數(shù),該線程函數(shù)的參數(shù)是該線程類對(duì)象的this指針?。?!


class?Servant
{
	private:

		pthread_mutex_t?mutex_x;
		pthread_cond_t?cond_t;
		int?state;
	public:
		pthread_t?m_tid;
		Servant();
		void?start();
	static?void*?ThreadProc(void*?data);
		void?notify();
		void?stop();
		void?wakeup();
		void?exit();
		void?join();
};

這是該線程類的聲明,最主要的是查看ThreadProc靜態(tài)函數(shù)



void*?Servant::ThreadProc(void*?data)
{????
????Servant*?ser=(Servant*)data;
????int?result=0;
????while(ser->state!=EXIT)
????{
????????while(ser->state==IDLE)
????????{
????????????result=pthread_mutex_lock(&(ser->mutex_x));
????????????if(0==result)
????????????{
????????????????printf("waiting?for?the?mutex?n");
????????????}????????????
????????????result=pthread_cond_wait(&(ser->cond_t),&(ser->mutex_x));????
????????????if(ser->state!=IDLE)
????????????????goto?End;
????????????????
????????????printf("the?conditions?has?been?notifiedn");
????????????//we?take?this?thread?to?busy?list
????????????ser->m_pool->AddtoIdle(false,ser);
????????????ser->m_pool->Addtobusy(true,ser);
????????????//?really???work
????????????
????????????DoSomething:
????????????ser->state=WORKING;????????????
????????????printf("Do?Something...n");
????????????ser->DoJob();
????????????ser->Complete();//this?function?change?state
????????????End:
????????????pthread_mutex_unlock(&(ser->mutex_x));????????????????????
????????}
????}
????return?NULL;
}

這函數(shù),主要控制的就兩項(xiàng),其一state,每次對(duì)線程狀態(tài)的改變,都是基于原子線程已經(jīng)執(zhí)行完畢的基礎(chǔ)上的,也即,核心操作是不能被狀態(tài)的改變所修改的。每次被喚醒時(shí),都需要進(jìn)行檢測(cè)state的值,如果STATE還是RUN,則表明有新的任務(wù)要處理,否則表明喚醒只是為了要修改當(dāng)前線程狀態(tài)?。?!,現(xiàn)在看一下一個(gè)API


pthread_cond_wait,該函數(shù)調(diào)用時(shí)首先必須必須由本線程加鎖,這個(gè)函數(shù)需要兩個(gè)變量一個(gè)是mutex_x(加鎖的互斥變量)和cond_t(條件變量),在更新條件以前,mutex_x一直保持鎖定狀態(tài),并在線程掛起進(jìn)入等待解鎖,當(dāng)執(zhí)行pthread_cond_singal時(shí),激活互斥變量mutex,并向下執(zhí)行,當(dāng)離開(kāi)pthread_cond_wait時(shí),重新加鎖

現(xiàn)Servant.h文件內(nèi)容如下:


#include#includeusing?namespace?std;
class?Servant
{
	private:

		pthread_mutex_t?mutex_x;
		pthread_cond_t?cond_t;
		int?state;
	public:
		pthread_t?m_tid;
		Servant();
		void?start();
	static?void*?ThreadProc(void*?data);
		void?notify();
		void?stop();
		void?wakeup();
		void?exit();
		void?join();
};
//ServantPool?supposed?to?be?singlton
class?ServantPool
{
private:
	vectorm_idle_list;
	vectorm_busy_list;
	ServantPool(){};
	static?ServantPool?*?hInstance;
public:	
	void?TerminateAll();
	void?create(int?num);
	void?start();
	void?stop(pthread_t?id);
	void?wakeup(pthread_t?id);
	void?waitforAll();
	~ServantPool();	
	static?ServantPool?*?getInstance();	
};


Servant.cpp文件內(nèi)容如下:



#include#include#include#include#include#include#include#include"Servant.h"
#define?WORKING?1
#define?STOP?2
#define?EXIT?3
#define?IDLE?4
static?int?num=0;
static?????pthread_mutex_t?vec_mutex;?
void?Servant::start()
{
????state=IDLE;
????int?result=pthread_create(&m_tid,NULL,ThreadProc,this);
????if(0!=result)
????{
????????printf("thread?create?result?:%sn",strerror(errno));
????}
}
Servant::Servant(ServantPool*?ma)
{????
????thread_id=num++;
????m_pool=ma;????
????pthread_mutex_init(&mutex_x,NULL);
????pthread_cond_init(&cond_t,NULL);????
}
void?Servant::setSocket(int?m)
{
????m_socket=m;
}
void?Servant::DoJob()
{
????char?buf[100]={0};
????recv(m_socket,buf,100,0);
????printf("we?haved?the?%d?recv:%sn",num++,buf);
????close(m_socket);
}
void*?Servant::ThreadProc(void*?data)
{????
????Servant*?ser=(Servant*)data;
????int?result=0;
????while(ser->state!=EXIT)
????{
????????while(ser->state==IDLE)
????????{
????????????result=pthread_mutex_lock(&(ser->mutex_x));
????????????if(0==result)
????????????{
????????????????printf("waiting?for?the?mutex?n");
????????????}????????????
????????????result=pthread_cond_wait(&(ser->cond_t),&(ser->mutex_x));????
????????????if(ser->state!=IDLE)
????????????????goto?End;
????????????????
????????????printf("the?conditions?has?been?notifiedn");
????????????//we?take?this?thread?to?busy?list
????????????ser->m_pool->AddtoIdle(false,ser);
????????????ser->m_pool->Addtobusy(true,ser);
????????????//?really???work
????????????
????????????DoSomething:
????????????ser->state=WORKING;????????????
????????????printf("Do?Something...n");
????????????ser->DoJob();
????????????ser->Complete();//this?function?change?state
????????????End:
????????????pthread_mutex_unlock(&(ser->mutex_x));????????????????????
????????}
????}
????return?NULL;
}
void?Servant::stop()
{
????if(state==IDLE)????????
????{
????????m_pool->AddtoIdle(false,this);
????????m_pool->Addtostop(true,this);
????????state=STOP;
????????printf("thread?stop!n");????????
????}
????else?if(state==WORKING)
????{
????????printf("current?state?is?WORKING?stop?failed!n");????????????????
????}
????else?if(state==STOP)
????{
????????printf("thread?already?stopped!n");????????????????
????}
????else?
????{
????????printf("sorry?unknown?state!n");
????????state=STOP;
????}
}
void?Servant::wakeup()
{
????if(state==STOP)????????
????{
????????m_pool->Addtostop(false,this);
????????m_pool->AddtoIdle(true,this);
????????state=IDLE;????
????????printf("thread?wakeup!n");????????????
????}
????else?if(state==WORKING)
????{
????????printf("current?state??is?WORKING?stop?failed!n");
????}
????else?if(state==IDLE)
????{
????????printf("current?state??is?idle?never?need?wakeup!n");
????}
????else
????{
????????printf("sorry?unknown?state..n");
????????state=IDLE;
????}
}
void?Servant::Complete()//完成操作
{
????//完成任務(wù),該線程變?yōu)閕dle????????
????m_pool->Addtobusy(false,this);
????m_pool->AddtoIdle(true,this);????
????state=IDLE;
}
void?Servant::join()
{
????pthread_join(m_tid,NULL);
}
void?Servant::notify()
{
????if(state==IDLE)
????{????????
????????printf("we?have?notified?thread?runningn");
????????pthread_cond_signal(&cond_t);????
????}
????else
????{
????????printf("sorry?,the?signal?is?not?correctn");
????}
}
void?Servant::exit()
{
????state=EXIT;
????pthread_cond_signal(&cond_t);????
}




void?ServantPool::StopAll()
{????
????vector::iterator?itr=m_idle_list.begin();????
????for(;itr!=m_idle_list.end();)
????{
????????(*itr)->stop();????????
????}
????itr=m_busy_list.begin();
????for(;itr!=m_busy_list.end();)
????{
????????(*itr)->stop();????????
????}????
}
void?ServantPool::create(int?num)
{
????int?i=0;
????for(;i<num;i++)
????{
????????Servant*?tmp=new?Servant(this);
????????m_idle_list.push_back(tmp);
????}
}
void?ServantPool::AddtoIdle(bool?add,Servant*?ser)
{
????if(add)
????{
????????//?add?ser?to?idle?list
????????pthread_mutex_lock(&vec_mutex);
????????m_idle_list.push_back(ser);
????????pthread_mutex_unlock(&vec_mutex);
????}
????else
????{
????????//?del?ser?from?idle?list
????????pthread_mutex_lock(&vec_mutex);
????????vector::iterator?itr=m_idle_list.begin();
????????for(;itr!=m_idle_list.end();itr++)
????????{
????????????if(*itr==ser)
????????????{
????????????????m_idle_list.erase(itr);
????????????????break;
????????????}
????????}
????????pthread_mutex_unlock(&vec_mutex);
????}
}
void?ServantPool::Addtobusy(bool?add,Servant*?ser)
{
????if(add)
????{
????????//?add?ser?to?idle?list
????????pthread_mutex_lock(&vec_mutex);
????????m_busy_list.push_back(ser);
????????pthread_mutex_unlock(&vec_mutex);
????}
????else
????{
????????//?del?ser?from?idle?list
????????pthread_mutex_lock(&vec_mutex);
????????vector::iterator?itr=m_busy_list.begin();
????????for(;itr!=m_busy_list.end();itr++)
????????{
????????????if(*itr==ser)
????????????{
????????????????m_busy_list.erase(itr);
????????????????break;
????????????}
????????}
????????pthread_mutex_unlock(&vec_mutex);
????}
}
void?ServantPool::Addtostop(bool?add,Servant*?ser)
{
????if(add)
????{
????????//?add?ser?to?idle?list
????????pthread_mutex_lock(&vec_mutex);
????????m_stop_list.push_back(ser);
????????pthread_mutex_unlock(&vec_mutex);
????}
????else
????{
????????//?del?ser?from?idle?list
????????pthread_mutex_lock(&vec_mutex);
????????vector::iterator?itr=m_stop_list.begin();
????????for(;itr!=m_stop_list.end();itr++)
????????{
????????????if(*itr==ser)
????????????{
????????????????m_stop_list.erase(itr);
????????????????break;
????????????}
????????}
????????pthread_mutex_unlock(&vec_mutex);
????}
}
void?ServantPool::startAll()
{
????int?i=0;
????for(;istart();//create?the?thread
????}
}

void?ServantPool::stop(Servant*?id)
{????
????vector::iterator?itr=m_idle_list.begin();????
????for(;itr!=m_idle_list.end();itr++)
????{
????????if((*itr)==id)
????????{????
????????????(*itr)->stop();????????????
????????????return;
????????}
????}
}

void?ServantPool::waitforAll()
{
????int?i=0;
????int?nums=m_busy_list.size();
????for(;ijoin();
????}
????nums=m_idle_list.size();
????i=0;
????for(;ijoin();
????}????
}

void?ServantPool::wakeup(Servant*?id)
{????
????vector::iterator?itr=m_busy_list.begin();????
????for(;itr!=m_busy_list.end();itr++)
????{
????????if((*itr)==id)
????????{????
????????????(*itr)->wakeup();????????????
????????????return;
????????}
????}
}
ServantPool?*?ServantPool::hInstance=NULL;
ServantPool?*?ServantPool::getInstance()
{????
????if(NULL==hInstance)
????{
????????hInstance=new?ServantPool();
????????pthread_mutex_init(&vec_mutex,NULL);
????}
????return?hInstance;????????
}
ServantPool::~ServantPool()
{
????vector::iterator?itr=m_idle_list.begin();
????for(;itr!=m_idle_list.end();)
????{????????
????????(*itr)->exit();????????????????
????????delete?*itr;????????
????????itr=m_idle_list.erase(itr);????????
????}
????
????itr=m_busy_list.begin();
????for(;itr!=m_busy_list.end();)
????{????????
????????(*itr)->exit();????????????????
????????delete?*itr;????????
????????itr=m_busy_list.erase(itr);????????
????}????
????
????itr=m_stop_list.begin();
????for(;itr!=m_stop_list.end();)
????{????????
????????(*itr)->exit();????????????????
????????delete?*itr;????????
????????itr=m_stop_list.erase(itr);????????
????}????
}
Servant*?ServantPool::Accepting()
{
????if(m_idle_list.size()>0)
????{
????????return?m_idle_list[0];
????}
????return?NULL;
}


main.c文件如下:



#include#include#include#include#include"Servant.h"
//create?100?servants?thread
#define?SERVANT_NUM?1
int?main()
{

	ServantPool?*mm=ServantPool::getInstance();
	printf("we?begin?create?%d?Servant?threads?n",SERVANT_NUM);
	mm->create(SERVANT_NUM);
	mm->start();
	mm->waitforAll();
	return?0;
}


makefile文件內(nèi)容如下:



test:Servant.o?main.o
	g++?-o???test?Servant.o?main.o??-lpthread
Servant.o:Servant.cpp?Servant.h
	g++?-g?-c??Servant.cpp?-lpthread
main.o:main.c?Servant.h???
	g++?-g?-c??main.c?-lpthread
clean:
	rm?*.o?test

這里面需要注意一點(diǎn),就是,在修改線程狀態(tài)時(shí),首先應(yīng)該修改的是線程狀態(tài)值,然后才繼續(xù)進(jìn)行信號(hào)通知,這樣可以避免,當(dāng)信號(hào)通知時(shí),狀態(tài)值還沒(méi)有發(fā)生變化?。。?/p>

信號(hào)修改失敗。好了,這就是暫時(shí)要表達(dá)的線程池的內(nèi)容了,如有錯(cuò)誤,該請(qǐng)大神指正



本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車(chē)的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車(chē)技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車(chē)工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車(chē)。 SODA V工具的開(kāi)發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車(chē) 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開(kāi)幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉