c語言線程關閉函數(shù) c語言關閉進程

C語言 殺死線程 Api函數(shù)

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

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)福清免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉變。

C語言如何讓結束指定進程中的指定線程

終止線程有三種方法:

1.線程可以在自身內(nèi)部調(diào)用AfxEndThread()來終止自身的運行

2.可以在線程的外部調(diào)用BOOL TerminateThread( HANDLE hThread, DWORD dwExitCode )來強行終止一個線程的運行,

然后調(diào)用CloseHandle()函數(shù)釋放線程所占用的堆棧

3.第三種方法是改變?nèi)肿兞?,使線程的執(zhí)行函數(shù)返回,則該線程終止。

unsigned long __cdecl _beginthread (void (__cdecl *) (void *),

unsigned, void *); PS--這是我復制的別人的

請問C語言中怎樣結束主函數(shù)的運行?

給你兩種方法

(1)return法

如果main函數(shù)沒有返回值,則

return;有則返回相應類型的值即可

(2)ctr+Z或者ctr+D,前者為微軟操作系統(tǒng),后者是Unix系統(tǒng)中停止程序的運行

c語言 退出整個程序或函數(shù)的命令是什么

c語言退出整個程序或函數(shù)的命令是return、goto 、break 、break。

1、return 返回;

return 表示從被調(diào)用函數(shù)返回主調(diào)函數(shù)繼續(xù)執(zhí)行,返回時可附帶一個返回值,由return后面的參數(shù)設定。

2、goto 無條件跳轉;

goto語句也稱作無條件轉移語句,其一般格式為goto語句標號:其中語句標號是按照標識符規(guī)定書寫的符號,放在某一行語句行的前面,標號后加冒號(:)。

3、break 調(diào)處最近一層塊;

大多數(shù)情況下是終止上一層的循環(huán),C語言中break在switch中執(zhí)行一條case后跳出語句的作用 ?使程序跳出switch執(zhí)行switch以后的語句 如果沒有break switch會從滿足條件的地方執(zhí)行到switch結構結束。

擴展資料

break語句使用

示例:

#include stdio.h

void main()

{

int x=1;

while(x=4)

{

printf("x=%d\n",x);

if (x==3)

{

break;

}

x++;

}

}

C語言如何終止線程?

面只有兩個線程,是生產(chǎn)者/消費者模式,已編譯通過,注釋很詳細。

/* 以生產(chǎn)者和消費者模型問題來闡述Linux線程的控制和通信你

生產(chǎn)者線程將生產(chǎn)的產(chǎn)品送入緩沖區(qū),消費者線程則從中取出產(chǎn)品。

緩沖區(qū)有N個,是一個環(huán)形的緩沖池。

*/

#include stdio.h

#include pthread.h

#define BUFFER_SIZE 16

struct prodcons

{

int buffer[BUFFER_SIZE];/*實際存放數(shù)據(jù)的數(shù)組*/

pthread_mutex_t lock;/*互斥體lock,用于對緩沖區(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ū),這里是存入一個整數(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);//非空就設置條件變量notempty

}

/*讀書據(jù),移動讀指針*/

data = p-buffer[p-readpos];

p-readpos++;

if(p-readpos == BUFFER_SIZE)

p-readpos = 0;

/*設置緩沖區(qū)未滿的條件變量*/

pthread_cond_signal(p-notfull);

pthread_mutex_unlock(p-lock);

return data;

}

/*測試:生產(chǎn)站線程將1 到1000的整數(shù)送入緩沖區(qū),消費者線程從緩沖區(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);

/*等待兩個線程結束*/

pthread_join(th_p, retval);

pthread_join(th_c,retval);

return 0;

}

c語言怎么創(chuàng)建線程和使用

1、添加線程相關的頭文件:#includepthread.h

2、線程創(chuàng)建函數(shù)是pthread_create()函數(shù),該函數(shù)的原型為:

int?pthread_create(pthread_t?*thread,pthread_attr_t?*attr,void*?(*start_routine)(void*),void?*arg);

3、線程退出函數(shù)是pthread_exit()函數(shù),該函數(shù)的原型為:

void?pthread_exit(void?*retval);

創(chuàng)建線程的示例程序如下:

/*

**程序說明:創(chuàng)建線程函數(shù)pthread_create()函數(shù)的使用。

*/

#include?stdio.h

#include?pthread.h

#include?unistd.h

#include?stdlib.h

#include?string.h

//打印標識符的函數(shù)

void?print_ids(const?char?*str)

{

pid_t?pid; //進程標識符

pthread_t?tid; //線程標識符

pid=getpid(); //獲得進程號

tid=pthread_self(); //獲得線程號

printf("%s?pid:%u?tid:%u?(0x%x)\n",

str,(unsigned?int)pid,(unsigned?int)tid,(unsigned?int)tid);?//打印進程號和線程號

}

//線程函數(shù)

void*?pthread_func(void?*arg)

{

print_ids("new?thread:"); //打印新建線程號

return?((void*)0);

}

//主函數(shù)

int?main()

{

int?err;

pthread_t?ntid; //線程號

err=pthread_create(ntid,NULL,pthread_func,NULL); //創(chuàng)建一個線程

if(err?!=?0)

{

printf("create?thread?failed:%s\n",strerror(err));

exit(-1);

}

print_ids("main?thread:"); //打印主線程號

sleep(2);

return?0;

}

網(wǎng)站題目:c語言線程關閉函數(shù) c語言關閉進程
標題路徑:http://www.muchs.cn/article16/doseogg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站軟件開發(fā)、品牌網(wǎng)站建設、自適應網(wǎng)站、小程序開發(fā)云服務器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設