GetExitCodeThread函數(shù)可以得到線程終結(jié)時(shí)的返回值,因?yàn)榫€程終結(jié)了,所以系統(tǒng)中不再有這個(gè)線程,hThreadRcvData本身已經(jīng)無效,所以調(diào)用TerminateThread函數(shù)會(huì)失敗。

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括趙縣網(wǎng)站建設(shè)、趙縣網(wǎng)站制作、趙縣網(wǎng)頁制作以及趙縣網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,趙縣網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到趙縣省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
有三種方式可以終止線程,具體調(diào)用函數(shù)依賴于使用的線程系統(tǒng)。
1 在線程入口函數(shù)中,調(diào)用return。 即退出線程入口函數(shù),可以實(shí)現(xiàn)終止當(dāng)前線程效果;
2 在線程執(zhí)行的任意函數(shù),調(diào)用當(dāng)前線程退出函數(shù),可以退出當(dāng)前線程;
3 在任意位置,調(diào)用線程終止函數(shù),并傳入要終止線程的標(biāo)識(shí)符,即pid,可以實(shí)現(xiàn)終止對(duì)應(yīng)線程效果。
終止線程有三種方法:
1.線程可以在自身內(nèi)部調(diào)用AfxEndThread()來終止自身的運(yùn)行
2.可以在線程的外部調(diào)用BOOL TerminateThread( HANDLE hThread, DWORD dwExitCode )來強(qiáng)行終止一個(gè)線程的運(yùn)行,
然后調(diào)用CloseHandle()函數(shù)釋放線程所占用的堆棧
3.第三種方法是改變?nèi)肿兞浚咕€程的執(zhí)行函數(shù)返回,則該線程終止。
unsigned long __cdecl _beginthread (void (__cdecl *) (void *),
unsigned, void *); PS--這是我復(fù)制的別人的
面只有兩個(gè)線程,是生產(chǎn)者/消費(fèi)者模式,已編譯通過,注釋很詳細(xì)。
/* 以生產(chǎn)者和消費(fèi)者模型問題來闡述Linux線程的控制和通信你
生產(chǎn)者線程將生產(chǎn)的產(chǎn)品送入緩沖區(qū),消費(fèi)者線程則從中取出產(chǎn)品。
緩沖區(qū)有N個(gè),是一個(gè)環(huán)形的緩沖池。
*/
#include stdio.h
#include pthread.h
#define BUFFER_SIZE 16
struct prodcons
{
int buffer[BUFFER_SIZE];/*實(shí)際存放數(shù)據(jù)的數(shù)組*/
pthread_mutex_t lock;/*互斥體lock,用于對(duì)緩沖區(qū)的互斥操作*/
int readpos,writepos; /*讀寫指針*/
pthread_cond_t notempty;/*緩沖區(qū)非空的條件變量*/
pthread_cond_t notfull;/*緩沖區(qū)未滿 的條件變量*/
};
/*初始化緩沖區(qū)*/
void pthread_init( struct prodcons *p)
{
pthread_mutex_init(p-lock,NULL);
pthread_cond_init(p-notempty,NULL);
pthread_cond_init(p-notfull,NULL);
p-readpos = 0;
p-writepos = 0;
}
/*將產(chǎn)品放入緩沖區(qū),這里是存入一個(gè)整數(shù)*/
void put(struct prodcons *p,int data)
{
pthread_mutex_lock(p-lock);
/*等待緩沖區(qū)未滿*/
if((p-writepos +1)%BUFFER_SIZE ==p-readpos)
{
pthread_cond_wait(p-notfull,p-lock);
}
p-buffer[p-writepos] =data;
p-writepos++;
if(p-writepos = BUFFER_SIZE)
p-writepos = 0;
pthread_cond_signal(p-notempty);
pthread_mutex_unlock(p-lock);
}
/*從緩沖區(qū)取出整數(shù)*/
int get(struct prodcons *p)
{
int data;
pthread_mutex_lock(p-lock);
/*等待緩沖區(qū)非空*/
if(p-writepos == p-readpos)
{
pthread_cond_wait(p-notempty ,p-lock);//非空就設(shè)置條件變量notempty
}
/*讀書據(jù),移動(dòng)讀指針*/
data = p-buffer[p-readpos];
p-readpos++;
if(p-readpos == BUFFER_SIZE)
p-readpos = 0;
/*設(shè)置緩沖區(qū)未滿的條件變量*/
pthread_cond_signal(p-notfull);
pthread_mutex_unlock(p-lock);
return data;
}
/*測(cè)試:生產(chǎn)站線程將1 到1000的整數(shù)送入緩沖區(qū),消費(fèi)者線程從緩沖區(qū)中獲取整數(shù),兩者都打印信息*/
#define OVER (-1)
struct prodcons buffer;
void *producer(void *data)
{
int n;
for( n=0;n1000;n++)
{
printf("%d ------\n",n);
put(buffer,n);
}
put(buffer,OVER);
return NULL;
}
void *consumer(void *data)
{
int d;
while(1)
{
d = get(buffer);
if(d == OVER)
break;
else
printf("-----%d\n",d);
}
return NULL;
}
int main()
{
pthread_t th_p,th_c;
void *retval;
pthread_init(buffer);
pthread_create(th_p,NULL,producer,0);
pthread_create(th_c,NULL,consumer,0);
/*等待兩個(gè)線程結(jié)束*/
pthread_join(th_p, retval);
pthread_join(th_c,retval);
return 0;
}
當(dāng)前題目:c語言銷毀線程函數(shù),C#銷毀線程
文章URL:http://chinadenli.net/article41/dsepghd.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站建設(shè)、云服務(wù)器、企業(yè)網(wǎng)站制作、用戶體驗(yàn)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)