新聞中心
Linux上的多線程編程在實踐中非常常見,但它也會帶來一些問題。其中的一個問題是如何等待線程結(jié)束。在本文中,我們將探討如何做到這一點以及一些注意事項。

岳陽網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,岳陽網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為岳陽1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的岳陽做網(wǎng)站的公司定做!
1.線程的不同類型
在開始討論如何等待線程結(jié)束之前,我們需要了解一些線程的基礎(chǔ)知識。在Linux中,有幾種不同類型的線程。具體來說,有用戶級線程和內(nèi)核級線程。用戶級線程是由用戶空間的線程庫(DLL)實現(xiàn)的,而內(nèi)核級線程是由操作系統(tǒng)內(nèi)核實現(xiàn)的。明白了這一點,我們就可以了解到,等待線程結(jié)束有兩種方法:一種是使用PTHREAD_JOIN()系統(tǒng)調(diào)用,另一種是使用等待(pthread_cond_wt())和通知(pthread_cond_signal())機制。
2.PTHREAD_JOIN()
PTHREAD_JOIN()系統(tǒng)調(diào)用是等待線程結(jié)束的一種方法。當調(diào)用這個系統(tǒng)調(diào)用時,當前線程會阻塞,直到指定的線程結(jié)束為止。這個系統(tǒng)調(diào)用的參數(shù)是要等待的線程的ID,第二個參數(shù)是線程返回值的指針。這種方法非常直觀,但也有一些問題。如果我們希望在被等待的線程結(jié)束之前,當前線程也能夠終止,則需要重新設(shè)置當前線程的信號處理器。此外,如果被等待的線程已經(jīng)以分離(也就是說,它已經(jīng)處于非joinable狀態(tài)),那么我們將無法等待它結(jié)束。
3.等待和通知(pthread_cond_wt() 和 pthread_cond_signal())
等待和通知機制是等待線程結(jié)束的另一種方法。在這種機制下,主線程將monitor結(jié)構(gòu)作為監(jiān)視器條件,并通過pthread_cond_wt()函數(shù)對其進行等待。被等待的線程對monitor結(jié)構(gòu)進行修改,并在完成任務(wù)后,通過pthread_cond_signal()函數(shù)將等待的線程喚醒。這種方法需要使用pthread_mutex_lock()函數(shù)以獲得同步訪問權(quán)限,以防止競爭條件,并確保只有一個線程可以訪問共享數(shù)據(jù)。
4.注意事項
無論使用哪種方法,都需要遵循一些注意事項。一定要確保在等待線程結(jié)束時,UI線程不會被阻塞。這可以通過使用非阻塞算法來實現(xiàn)。在使用等待和通知機制時,請確保防止競爭條件,以免導(dǎo)致死鎖。如果可能的話,盡量將線程設(shè)計得簡單明了。這樣做可以讓其易于維護,并減少需要等待線程結(jié)束時的工作量。
5.
無論是使用PTHREAD_JOIN()系統(tǒng)調(diào)用還是等待和通知機制,等待線程結(jié)束都需要仔細考慮。需要確保遵守更佳實踐,避免競爭條件,以確保代碼的可維護性和可靠性。如果您正確地實現(xiàn)了等待線程結(jié)束的機制,則可以更好地控制并發(fā)性,并提高代碼的可擴展性。
相關(guān)問題拓展閱讀:
- C++在linux下怎么多線程
- linux如何停止線程
C++在linux下怎么多線程
#ifndef THREAD_H_
#define THREAD_H_
#include
#include
class Runnable
{
public:
//運行實體
virtual void run() = 0;
};
//線程類
class Thread: public Runnable
{
private:
//線程初始化號
static int thread_init_number;
//當前線程初始化序號
int current_thread_init_number;
//線程體
Runnable *target;
//當前線程的線程ID
pthread_t tid;
//線程的狀態(tài)
int thread_status;
//線程屬性
pthread_attr_t attr;
//線咐唯彎程優(yōu)先級
sched_param param;
//獲取執(zhí)行方法的指針
static void* run0(void* pVoid);
//內(nèi)部執(zhí)行方法
void* run1();
//獲取線程序號
static int get_next_thread_num();
public:
//線程的狀態(tài)-新建
static const int THREAD_STATUS_NEW = 0;
//線程的狀態(tài)-正在運行
static const int THREAD_STATUS_RUNNING = 1;
//線程的狀態(tài)-運行結(jié)束
static const int THREAD_STATUS_EXIT = -1;
//構(gòu)造函數(shù)
Thread();
//構(gòu)造函數(shù)
Thread(Runnable *target);
//析構(gòu)
~Thread();
//線程的運行體
void run();
//開始執(zhí)行線程
bool start();
//獲取線程狀態(tài)
int get_state();
//等待線程直至退出
void join();
//等待線程退出或者超時
void join(unsigned long millis_time);
//比較兩個線程時候相同,通過current_thread_init_number判斷
bool operator ==(const Thread* other_pthread);
//獲取this線程ID
pthread_t get_thread_id();
//獲取當前線程ID
static pthread_t get_current_thread_id();
//當前線程是否和某個線程相等,通過tid判斷
static bool is_equals(Thread* iTarget);
//設(shè)置線程的類型:綁定/非綁山型定
void set_thread_scope(bool isSystem);
//獲取線程的類型:綁定/非綁定
bool get_thread_scope();
//設(shè)置線程的優(yōu)先級,1-99,其中99為實時,意外的為普通
void set_thread_priority(int priority);
//獲取線程的優(yōu)先級
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;
}
#endif /* THREAD_H_ */
與c語言一樣,使用線猛冊舉程庫,pthread線程,例如
#include
#include
#include
struct member
{
int num;
char *name;
};
//結(jié)構(gòu)體后的分號勿漏
void *create(void *arg)
//有void* 型參數(shù)傳入,不能直接void
{
struct member *temp;
temp=(struct member *)arg;
//結(jié)構(gòu)體變量之間不能直接賦值,但可以通過指針賦地址
printf(“member->枝碧num:%d\n”,temp->num);
printf(“member->name:%s\n”,temp->name);
sleep(1);
return (void *)8;
//這個很有特色,返回一個指向void的數(shù)據(jù)類型的值,這個值作為后面的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”;
//字符串賦值,其他好用簡便的方法有: char *p = NULL; p = new char ;
if((pthread_create(&tidp,NULL,create,(void*)b))==-1) /
//
void *
為“無類型指針”,void * 可以指向任何類型的數(shù)據(jù)
{
printf(“create error!\n”);
return 1;
}
if(pthread_join(tidp,&a))
//調(diào)用
pthread_join函數(shù),等待線程結(jié)束再繼續(xù)往下執(zhí)行,要不然主姿猜進程和下面的線程并行執(zhí)行
{
printf(“thread is not exit…\n”);
return -2;
}
printf(“thread is exit ,code is %d\n”,(int)a);//不知為啥這里是(int)a,,a不是指針來的么
return 0;
linux如何停止線程
先看進程號,然后用KILL命令!樓上正解!
殺死線鍵頃程 所在的進程就可以,
ps aux | grep 進程名
kill -TERM 進程號
如果你指的斗亮帆寫程序,空雹 那就參考 man pthread_exit
關(guān)于linux等待線程結(jié)束的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都創(chuàng)新互聯(lián)建站主營:成都網(wǎng)站建設(shè)、網(wǎng)站維護、網(wǎng)站改版的網(wǎng)站建設(shè)公司,提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、成都網(wǎng)站推廣、成都網(wǎng)站優(yōu)化seo、響應(yīng)式移動網(wǎng)站開發(fā)制作等網(wǎng)站服務(wù)。
網(wǎng)站欄目:Linux多線程編程:如何等待線程結(jié)束?(linux等待線程結(jié)束)
文章路徑:http://fisionsoft.com.cn/article/cddchdi.html


咨詢
建站咨詢
