新聞中心
Linux子線程不阻塞主線程:如何實現(xiàn)?

為茄子河等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及茄子河網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、成都做網(wǎng)站、茄子河網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
在編程中,多線程技術(shù)能夠很好地提高程序的效率,使得程序能夠更快地響應(yīng)用戶的操作。然而在使用多線程技術(shù)時,會遇到一些線程之間相互阻塞的問題,這會影響程序的運行效率。本文將介紹如何實現(xiàn)在Linux環(huán)境下,子線程不阻塞主線程的方法。
一、 什么是子線程不阻塞主線程
在許多情況下,主線程在等待子線程結(jié)束時,會被子線程阻塞。這樣會導致主線程執(zhí)行的時間變長,導致程序的效率下降。因此,如果子線程能夠在主線程不阻塞的情況下執(zhí)行,那么就可以大大提高程序的效率。
二、 如何實現(xiàn)子線程不阻塞主線程
在Linux環(huán)境下,實現(xiàn)子線程不阻塞主線程有多種方法,下面分別介紹。
1. 使用非阻塞I/O
使用非阻塞I/O是Linux下實現(xiàn)子線程不阻塞主線程的一種方法。非阻塞I/O的方式是在I/O調(diào)用時告訴操作系統(tǒng),不要強制等待I/O操作的完成,而是立即返回。這種方式能夠使得主線程在等待I/O時,不會被阻塞,從而達到子線程不阻塞主線程的效果。
2. 使用多線程編程框架
使用多線程編程框架也是一種比較常見的方法。其中比較流行的多線程編程框架有OpenMP和Pthreads。使用多線程編程框架可以讓程序執(zhí)行更加高效。
3. 使用信號量機制
使用信號量機制也是Linux下實現(xiàn)子線程不阻塞主線程的一種方式。信號量是一個計數(shù)器,用來實現(xiàn)進程之間的同步和互斥。主線程可以在需要的地方掛起等待子線程發(fā)出的信號,從而實現(xiàn)子線程不阻塞主線程。
以上三種方法在實現(xiàn)子線程不阻塞主線程方面都有很好的效果,可以根據(jù)實際情況選擇使用。
三、 結(jié)束語
在編寫程序時,使用多線程技術(shù)可以提高程序的效率,但是線程間的阻塞問題也需要引起足夠的注意。在Linux環(huán)境下,使用非阻塞I/O、多線程編程框架和信號量機制都可以實現(xiàn)子線程不阻塞主線程的效果。我們應(yīng)該在實際應(yīng)用中根據(jù)需求選擇適當?shù)姆椒▉斫鉀Q線程間的阻塞問題,從而使得程序更加高效。
相關(guān)問題拓展閱讀:
- linux怎么查看線程阻塞原因
- Linux多進程和線程同步的幾種方式
linux怎么查看線程阻塞原因
linux查看線程阻塞原因:
pthread_join
一般肆正衡主線程來調(diào)用,用來等待子線程退出,因為是等待,所以是阻塞的,一般主線程會依次join所有它創(chuàng)建的子線程。
1)執(zhí)行top命令,或使用裂做-H選項(顯示所有線程),找到相關(guān)的高CPU的PID。
2)生成thread dump
快照
(kill -3 PID)。
3)將top命令輸出PID轉(zhuǎn)換為HEX格式(
16進制
)。
4)在thread dump data中搜索nid=。
5)分析受影響的thread和stack trace,精確定位代碼。
特點:
Linux,全稱GNU/Linux,是一套免費使用和自由傳播的類Unix操作系統(tǒng),是一個基于POSIX的多用戶、多任務(wù)、支持
多線程
和多CPU的操作系統(tǒng)。伴隨著互聯(lián)網(wǎng)的發(fā)展,Linux得到了來自全世界軟件愛好者、組織、公司的支持。
它除了在服務(wù)器方面保持著強勁的發(fā)展勢頭以外,在個人電腦、
嵌入式系統(tǒng)
上都有著長足的進步。使用者不僅可以直觀地獲取該操作系統(tǒng)的實現(xiàn)機制,而且可以根據(jù)自身的需要來修改完善Linux,使其更大化地適應(yīng)用戶的需要。
Linux不僅系統(tǒng)性能穩(wěn)定,而且是
開源軟件
。其核心防火墻組件性能高效、配置簡單,保證了系統(tǒng)的安全。在很多企業(yè)網(wǎng)絡(luò)中,為了追求速度和安全清敏,Linux不僅僅是被網(wǎng)絡(luò)運維人員當作服務(wù)器使用,甚至當作網(wǎng)絡(luò)防火墻,這是Linux的一大亮點。
Linux具有
開放源碼
、沒有版權(quán)、技術(shù)社區(qū)用戶多等特點,開放源碼使得用戶可以自由裁剪,靈活性高,功能強大,成本低。尤其系統(tǒng)中內(nèi)嵌網(wǎng)絡(luò)
協(xié)議棧
,經(jīng)過適當?shù)呐渲镁涂蓪崿F(xiàn)路由器的功能。這些特點使得Linux成為開發(fā)路由交換設(shè)備的理想開發(fā)平臺。
Linux多進程和線程同步的幾種方式
Linux 線程同步的橘螞笑三種方法
線程的更大特點是資源的共享性,但資源共享中的同步問題是多線程編程的難點。linux下提供了多種方式來處理線程同步,最常用的是互斥鎖、條件變量和信號量。
一、互斥鎖(mutex)
通過鎖機制實現(xiàn)線程間的同步。
初始化鎖。在Linux下,線程的互斥量數(shù)據(jù)類型是pthread_mutex_t。在使用前,要對它進行初始化。
靜態(tài)分配:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
動態(tài)分配:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);
加鎖。對共享資源的訪問,要對互斥量進行加鎖,如果互斥量已經(jīng)上了鎖,調(diào)用線程會阻塞,直到互斥量被解鎖。
int pthread_mutex_lock(pthread_mutex *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
解鎖。在完成了對共享資源的訪問后,要對互斥量進行解鎖。
int pthread_mutex_unlock(pthread_mutex_t *mutex);
銷毀鎖。鎖在是使用完成后,需要進行銷毀以釋放資源。
int pthread_mutex_destroy(pthread_mutex *mutex);
view plain copy
#include
#include
#include
#include
#include “iostream”
using namespace std;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int tmp;
void* thread(void *arg)
{
cout
#include
#include “stdlib.h”
#include “unistd.h”
pthread_mutex_t mutex;
pthread_cond_t cond;
void hander(void *arg)
{
free(arg);
(void)pthread_mutex_unlock(&mutex);
}
void *thread1(void *arg)
{
pthread_cleanup_push(hander, &mutex);
while(1)
{
printf(“thread1 is running\n”);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf(“thread1 applied the condition\n”);
pthread_mutex_unlock(&mutex);
sleep(4);
}
pthread_cleanup_pop(0);
}
void *thread2(void *arg)
{
while(1)
{
printf(“thread2 is running\n”);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf(“thread2 applied the condition\n”);
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
pthread_t thid1,thid2;
printf(“condition variable study!\n”);
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&thid1, NULL, thread1, NULL);
pthread_create(&thid2, NULL, thread2, NULL);
sleep(1);
do
{
pthread_cond_signal(&cond);
}while(1);
sleep(20);
pthread_exit(0);
return 0;
}
view plain copy
#include
#include
#include “stdio.h”
#include “stdlib.h”
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct node
{
int n_number;
struct node *n_next;
}*head = NULL;
static void cleanup_handler(void *arg)
{
printf(“Cleanup handler of second thread./n”);
free(arg);
(void)pthread_mutex_unlock(&mtx);
}
static void *thread_func(void *arg)
{
struct node *p = NULL;
pthread_cleanup_push(cleanup_handler, p);
while (1)
{
//這個mutex主要是用來保證pthread_cond_wait的并發(fā)性
pthread_mutex_lock(&mtx);
while (head == NULL)
{
//這個while要特別說明一下,單個pthread_cond_wait功能很完善,為何
//這里要有一個while (head == NULL)呢?因為pthread_cond_wait里的線
//程可能會被意外喚醒,如果這個時候head != NULL,則不是我們想要的情況。
//這個時候,應(yīng)該讓線程繼續(xù)進入pthread_cond_wait
// pthread_cond_wait會先解除之前的pthread_mutex_lock鎖定的mtx,
//然后阻塞在等待對列里休眠,直到再次被喚醒(大多數(shù)情況下是等待的條件成立
//而被喚醒,喚醒后,該進程會先鎖定先pthread_mutex_lock(&mtx);,再讀取資源
//用這個流程是比較清楚的
pthread_cond_wait(&cond, &mtx);
p = head;
head = head->n_next;
printf(“Got %d from front of queue/n”, p->n_number);
free(p);
}
pthread_mutex_unlock(&mtx); //臨界區(qū)數(shù)據(jù)操作完畢,釋放互斥鎖
}
pthread_cleanup_pop(0);
return 0;
}
int main(void)
{
pthread_t tid;
int i;
struct node *p;
//子線程會一直等待資源,類似生產(chǎn)者和消費者,但是這里的消費者可以是多個消費者,而
//不僅僅支持普通的單個消費者,這個模型雖然簡單,但是很強大
pthread_create(&tid, NULL, thread_func, NULL);
sleep(1);
for (i = 0; i n_number = i;
pthread_mutex_lock(&mtx); //需要操作head這個臨界資源,先加鎖,
p->n_next = head;
head = p;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx); //解鎖
sleep(1);
}
printf(“thread 1 wanna end the line.So cancel thread 2./n”);
//關(guān)于pthread_cancel,有一點額外的說明,它是從外部終止子線程,子線程會在最近的取消點,退出
//線程,而在我們的代碼里,最近的取消點肯定就是pthread_cond_wait()了。
pthread_cancel(tid);
pthread_join(tid, NULL);
printf(“All done — exiting/n”);
return 0;
}
三、信號量(sem)
如同進程一樣,線程也可以通過信號量來實現(xiàn)通信,雖然是輕量級的。信號量函數(shù)的名字都以”sem_”打頭。線程使用的基本信號量函數(shù)有四個。
信號量初始化。
int sem_init (sem_t *sem , int pshared, unsigned int value);
這是對由sem指定的信號量進行初始化,設(shè)置好它的共享選項(linux 只支持為0,即表示它是當前進程的局部信號量),然后給它一個初始值VALUE。
等待信號量。給信號量減1,然后等待直到信號量的值大于0。
int sem_wait(sem_t *sem);
釋放信號量。信號量值加1。并通知其他等待線程。
int sem_post(sem_t *sem);
銷毀信號量。我們用完信號量后都它進行清理。歸還占有的一切資源。
int sem_destroy(sem_t *sem);
view plain copy
#include
#include
#include
#include
#include
#include
#define return_if_fail(p) if((p) == 0){printf (“:func error!/n”, __func__);return;}
typedef struct _PrivInfo
{
sem_t s1;
sem_t s2;
time_t end_time;
}PrivInfo;
static void info_init (PrivInfo* thiz);
static void info_destroy (PrivInfo* thiz);
static void* pthread_func_1 (PrivInfo* thiz);
static void* pthread_func_2 (PrivInfo* thiz);
int main (int argc, char** argv)
{
pthread_t pt_1 = 0;
pthread_t pt_2 = 0;
int ret = 0;
PrivInfo* thiz = NULL;
thiz = (PrivInfo* )malloc (sizeof (PrivInfo));
if (thiz == NULL)
{
printf (“: Failed to malloc priv./n”);
return -1;
}
info_init (thiz);
ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);
if (ret != 0)
{
perror (“pthread_1_create:”);
}
ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);
if (ret != 0)
{
perror (“pthread_2_create:”);
}
pthread_join (pt_1, NULL);
pthread_join (pt_2, NULL);
info_destroy (thiz);
return 0;
}
static void info_init (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
thiz->end_time = time(NULL) + 10;
sem_init (&thiz->s1, 0, 1);
sem_init (&thiz->s2, 0, 0);
return;
}
static void info_destroy (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
sem_destroy (&thiz->s1);
sem_destroy (&thiz->s2);
free (thiz);
thiz = NULL;
return;
}
static void* pthread_func_1 (PrivInfo* thiz)
{
return_if_fail(thiz != NULL);
while (time(NULL) end_time)
{
sem_wait (&thiz->s2);
printf (“pthread1: pthread1 get the lock./n”);
sem_post (&thiz->s1);
printf (“pthread1: pthread1 unlock/n”);
sleep (1);
}
return;
}
static void* pthread_func_2 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
while (time (NULL) end_time)
{
sem_wait (&thiz->s1);
printf (“pthread2: pthread2 get the unlock./n”);
sem_post (&thiz->s2);
printf (“pthread2: pthread2 unlock./n”);
sleep (1);
}
return;
}
創(chuàng)新互聯(lián)服務(wù)器托管擁有成都T3+級標準機房資源,具備完善的安防設(shè)施、三線及BGP網(wǎng)絡(luò)接入帶寬達10T,機柜接入千兆交換機,能夠有效保證服務(wù)器托管業(yè)務(wù)安全、可靠、穩(wěn)定、高效運行;創(chuàng)新互聯(lián)專注于成都服務(wù)器托管租用十余年,得到成都等地區(qū)行業(yè)客戶的一致認可。
文章題目:「Linux」子線程不阻塞主線程:如何實現(xiàn)?(linux子線程如何不阻塞主線程)
標題路徑:http://www.dlmjj.cn/article/djeghoj.html


咨詢
建站咨詢
