新聞中心
隨著現(xiàn)代計(jì)算機(jī)技術(shù)的發(fā)展,越來(lái)越多的應(yīng)用程序采用多線程并發(fā)的方式來(lái)提高性能和效率。然而,在多線程并發(fā)編程的過(guò)程中,線程的退出問(wèn)題一直困擾著程序員們。如果線程沒(méi)有正確地退出,會(huì)導(dǎo)致資源泄漏、死鎖等問(wèn)題,嚴(yán)重地影響程序的性能和穩(wěn)定性。因此,在,成為了程序員們迫切需要解決的問(wèn)題。

Linux下有多種方式可以等待線程退出,比較常用的方法包括pthread_join、pthread_cond_wt和pthread_barrier_wt。下面我們一一介紹它們的使用方法和優(yōu)缺點(diǎn)。
1. pthread_join
pthread_join函數(shù)是等待一個(gè)線程結(jié)束并回收其資源的函數(shù)。通過(guò)調(diào)用pthread_join函數(shù),調(diào)用線程會(huì)阻塞等待指定的線程結(jié)束,直到該線程退出并回收其占用的資源后,才會(huì)返回。在阻塞等待線程退出的過(guò)程中,調(diào)用線程可以繼續(xù)運(yùn)行其他的任務(wù),不會(huì)浪費(fèi)CPU資源。
pthread_join函數(shù)的參數(shù)包括等待的線程ID和線程退出狀態(tài)。如果線程成功退出,那么線程退出狀態(tài)為0;如果線程異常退出,那么線程退出狀態(tài)為一個(gè)非零值,可以通過(guò)errno獲取具體的錯(cuò)誤信息。如果需要等待多個(gè)線程結(jié)束,可以用一個(gè)循環(huán)結(jié)構(gòu)來(lái)連續(xù)調(diào)用pthread_join函數(shù)等待各個(gè)線程退出。
pthread_join函數(shù)的優(yōu)點(diǎn)是簡(jiǎn)單、易用,適合于等待線程數(shù)較少的情況。但是,如果等待的線程數(shù)較多,那么連續(xù)調(diào)用pthread_join函數(shù)會(huì)導(dǎo)致調(diào)用線程的阻塞時(shí)間較長(zhǎng),對(duì)程序的性能造成較大的影響。
2. pthread_cond_wt
pthread_cond_wt函數(shù)是等待條件變量的函數(shù),可以用于線程同步和線程間通信。通過(guò)調(diào)用pthread_cond_wt函數(shù),調(diào)用線程可以阻塞等待某個(gè)條件變量滿足,從而使得其他的線程可以修改和更新這個(gè)條件變量。
在使用pthread_cond_wt函數(shù)等待線程退出的過(guò)程中,需要使用互斥鎖pthread_mutex_t來(lái)保護(hù)條件變量。當(dāng)一個(gè)線程成功退出后,需要更新共享變量所表示的條件,然后通過(guò)調(diào)用pthread_cond_broadcast或pthread_cond_signal函數(shù)喚醒其他阻塞在條件變量上的線程。
pthread_cond_wt函數(shù)的優(yōu)點(diǎn)是可以有效地防止死鎖和資源泄漏。但是,在使用pthread_cond_wt函數(shù)等待線程退出的過(guò)程中,需要使用互斥鎖來(lái)保護(hù)條件變量,這會(huì)導(dǎo)致調(diào)用線程和其他線程之間的競(jìng)爭(zhēng),對(duì)程序的性能造成一定的影響。
3. pthread_barrier_wt
pthread_barrier_wt函數(shù)是等待一個(gè)屏障的函數(shù),可以用于多個(gè)線程之間的同步。通過(guò)調(diào)用pthread_barrier_wt函數(shù),多個(gè)線程都會(huì)阻塞在一個(gè)屏障上,直到所有的線程都到達(dá)了這個(gè)屏障后才會(huì)繼續(xù)執(zhí)行后面的代碼。
在使用pthread_barrier_wt函數(shù)等待線程退出的過(guò)程中,需要使用一個(gè)屏障pthread_barrier_t,每個(gè)線程到達(dá)屏障前都需要調(diào)用pthread_barrier_wt函數(shù)等待其他線程到達(dá)。當(dāng)所有的線程都到達(dá)屏障后,屏障會(huì)打開(kāi),并且所有的線程都會(huì)繼續(xù)執(zhí)行后面的代碼。
pthread_barrier_wt函數(shù)的優(yōu)點(diǎn)是可以有效地保證多個(gè)線程之間的同步,防止競(jìng)態(tài)條件的發(fā)生。但是,在使用pthread_barrier_wt函數(shù)等待線程退出的過(guò)程中,需要使用屏障變量來(lái)同步,這會(huì)導(dǎo)致程序的復(fù)雜性增加,需要對(duì)線程的邏輯結(jié)構(gòu)進(jìn)行重新設(shè)計(jì)。
綜上所述,在,取決于程序的具體實(shí)現(xiàn)和需要解決的問(wèn)題。如果需要等待的線程數(shù)較少,那么可以使用pthread_join函數(shù);如果需要等待多個(gè)線程退出,并且需要進(jìn)行線程間的通信和同步,那么可以使用pthread_cond_wt和pthread_barrier_wt函數(shù)。無(wú)論是哪種方式,都需要對(duì)線程的狀態(tài)進(jìn)行監(jiān)控和管理,防止資源泄漏和死鎖的發(fā)生,從而保證程序的性能和穩(wěn)定性。
成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián),建站經(jīng)驗(yàn)豐富以策略為先導(dǎo)10多年以來(lái)專(zhuān)注數(shù)字化網(wǎng)站建設(shè),提供企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),響應(yīng)式網(wǎng)站制作,設(shè)計(jì)師量身打造品牌風(fēng)格,熱線:028-86922220C++在linux下怎么多線程
與c語(yǔ)言一樣,使用線猛冊(cè)舉程庫(kù),pthread線程,例如
#include
#include
#include
struct member
{
int num;
char *name;
};
//結(jié)構(gòu)體后的分號(hào)勿漏
void *create(void *arg)
//有void* 型參數(shù)傳入,不能直接void
{
struct member *temp;
temp=(struct member *)arg;
//結(jié)構(gòu)體變量之間不能直接賦值,但可以通過(guò)指針賦地址
printf(“member->枝碧num:%d\n”,temp->num);
printf(“member->name:%s\n”,temp->name);
sleep(1);
return (void *)8;
//這個(gè)很有特色,返回一個(gè)指向void的數(shù)據(jù)類(lèi)型的值,這個(gè)值作為后面的exit code
}
int main(int agrc,char* argv)
{
pthread_t tidp;
struct member *b;
void* a;
b=(struct member *)malloc(sizeof(struct member));
//先分配內(nèi)存空間撒~
b->num=1;
b->name=”mlq”;
//字符串賦值,其他好用簡(jiǎn)便的方法有: char *p = NULL; p = new char ;
if((pthread_create(&tidp,NULL,create,(void*)b))==-1) /
//
void *
為“無(wú)類(lèi)型指針”,void * 可以指向任何類(lèi)型的數(shù)據(jù)
{
printf(“create error!\n”);
return 1;
}
if(pthread_join(tidp,&a))
//調(diào)用
pthread_join函數(shù),等待線程結(jié)束再繼續(xù)往下執(zhí)行,要不然主姿猜進(jìn)程和下面的線程并行執(zhí)行
{
printf(“thread is not exit…\n”);
return -2;
}
printf(“thread is exit ,code is %d\n”,(int)a);//不知為啥這里是(int)a,,a不是指針來(lái)的么
return 0;
}
#ifndef THREAD_H_
#define THREAD_H_
#include
#include
class Runnable
{
public:
//運(yùn)行實(shí)體
virtual void run() = 0;
};
//線程類(lèi)
class Thread: public Runnable
{
private:
//線程初始化號(hào)
static int thread_init_number;
//當(dāng)前線程初始化序號(hào)
int current_thread_init_number;
//線程體
Runnable *target;
//當(dāng)前線程的線程ID
pthread_t tid;
//線程的狀態(tài)
int thread_status;
//線程屬性
pthread_attr_t attr;
//線咐唯彎程優(yōu)先級(jí)
sched_param param;
//獲取執(zhí)行方法的指針
static void* run0(void* pVoid);
//內(nèi)部執(zhí)行方法
void* run1();
//獲取線程序號(hào)
static int get_next_thread_num();
public:
//線程的狀態(tài)-新建
static const int THREAD_STATUS_NEW = 0;
//線程的狀態(tài)-正在運(yùn)行
static const int THREAD_STATUS_RUNNING = 1;
//線程的狀態(tài)-運(yùn)行結(jié)束
static const int THREAD_STATUS_EXIT = -1;
//構(gòu)造函數(shù)
Thread();
//構(gòu)造函數(shù)
Thread(Runnable *target);
//析構(gòu)
~Thread();
//線程的運(yùn)行體
void run();
//開(kāi)始執(zhí)行線程
bool start();
//獲取線程狀態(tài)
int get_state();
//等待線程直至退出
void join();
//等待線程退出或者超時(shí)
void join(unsigned long millis_time);
//比較兩個(gè)線程時(shí)候相同,通過(guò)current_thread_init_number判斷
bool operator ==(const Thread* other_pthread);
//獲取this線程ID
pthread_t get_thread_id();
//獲取當(dāng)前線程ID
static pthread_t get_current_thread_id();
//當(dāng)前線程是否和某個(gè)線程相等,通過(guò)tid判斷
static bool is_equals(Thread* iTarget);
//設(shè)置線程的類(lèi)型:綁定/非綁山型定
void set_thread_scope(bool isSystem);
//獲取線程的類(lèi)型:綁定/非綁定
bool get_thread_scope();
//設(shè)置線程的優(yōu)先級(jí),1-99,其中99為實(shí)時(shí),意外的為普通
void set_thread_priority(int priority);
//獲取線程的優(yōu)先級(jí)
int get_thread_priority();
};
int Thread::thread_init_number = 1;
inline int Thread::get_next_thread_num()
{
return thread_init_number++;
}
void* Thread::run0(void* pVoid)
{
Thread* p = (Thread*) pVoid;
p->run1();
return p;
}
void* Thread::run1()
{
thread_status = THREAD_STATUS_RUNNING;
tid = pthread_self();
run();
thread_status = THREAD_STATUS_EXIT;
tid = 0;
pthread_exit(NULL);
}
void Thread::run()
{
if (target != NULL)
{
(*target).run();
}
} 衡悶
Thread::Thread()
{
tid = 0;
thread_status = THREAD_STATUS_NEW;
current_thread_init_number = get_next_thread_num();
pthread_attr_init(&attr);
}
Thread::Thread(Runnable *iTarget)
{
target = iTarget;
tid = 0;
thread_status = THREAD_STATUS_NEW;
current_thread_init_number = get_next_thread_num();
pthread_attr_init(&attr);
}
Thread::~Thread()
{
pthread_attr_destroy(&attr);
}
bool Thread::start()
{
return pthread_create(&tid, &attr, run0, this);
}
inline pthread_t Thread::get_current_thread_id()
{
return pthread_self();
}
inline pthread_t Thread::get_thread_id()
{
return tid;
}
inline int Thread::get_state()
{
return thread_status;
}
void Thread::join()
{
if (tid > 0)
{
pthread_join(tid,NULL);
}
}
void Thread::join(unsigned long millis_time)
{
if (tid == 0)
{
return;
}
if (millis_time == 0)
{
join();
}
else
{
unsigned long k = 0;
while (thread_status != THREAD_STATUS_EXIT && k tid;
}
void Thread::set_thread_scope(bool isSystem)
{
if (isSystem)
{
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
}
else
{
pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
}
}
void Thread::set_thread_priority(int priority)
{
pthread_attr_getschedparam(&attr,¶m);
param.__sched_priority = priority;
pthread_attr_setschedparam(&attr,¶m);
}
int Thread::get_thread_priority(){
pthread_attr_getschedparam(&attr,¶m);
return param.__sched_priority;
}
linux 進(jìn)程退出 線程怎么關(guān)閉
會(huì)的。例如段錯(cuò)誤之類(lèi)的錯(cuò)誤,直接會(huì)導(dǎo)致程序退出。 所以你要主線程還能正常運(yùn)行的,就要對(duì)子線程的錯(cuò)誤做錯(cuò)誤處理。
一般情況會(huì)自己關(guān)閉,如果很長(zhǎng)時(shí)間都還在,就強(qiáng)制kill -9
閨怨(王昌齡)
linux下怎么在等待線程結(jié)束中設(shè)置超時(shí)
我看過(guò)一個(gè)例子使用pthread_timedjoin_np可以,不過(guò)我在源蠢linux的森纖pthread庫(kù)里找不到這雹春陪條函數(shù)
timespec joinDelay;
joinDelay.tv_nsec = milliseconds;
pthread_timedjoin_np(thread, NULL, &joinDelay);
關(guān)于linux等待線程退出的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都創(chuàng)新互聯(lián)科技有限公司,經(jīng)過(guò)多年的不懈努力,公司現(xiàn)已經(jīng)成為一家專(zhuān)業(yè)從事IT產(chǎn)品開(kāi)發(fā)和營(yíng)銷(xiāo)公司。廣泛應(yīng)用于計(jì)算機(jī)網(wǎng)絡(luò)、設(shè)計(jì)、SEO優(yōu)化、關(guān)鍵詞排名等多種行業(yè)!
分享標(biāo)題:Linux下如何優(yōu)雅地等待線程退出(linux等待線程退出)
本文鏈接:http://m.5511xx.com/article/djhdihs.html


咨詢(xún)
建站咨詢(xún)
