新聞中心
隨著計算機技術的發(fā)展,程序的運行效率已經(jīng)成為了時刻追求的目標。而多線程技術作為其中的一種關鍵技術,成為了不可或缺的一部分。Linux作為一個優(yōu)秀的操作系統(tǒng),最早在多線程技術的研究和應用上成為了先驅,本文將會介紹Linux下的多線程實現(xiàn)以及如何進行編程來提高程序運行效率。

一、多線程技術介紹
多線程技術是一種能夠讓程序同時執(zhí)行多個任務的技術,在一個進程中可以同時創(chuàng)建多個線程,這些線程共享進程資源,相當于同時運行多個程序。相較于單線程的應用,多線程技術的優(yōu)勢體現(xiàn)在以下方面:
1. 程序執(zhí)行速度加快了
多線程可以讓程序同時處理多個任務,充分發(fā)揮了多核心CPU的性能,減少了空閑等待的時間,從而加快了程序的運行速度。
2. 提高系統(tǒng)資源利用率
多線程可以讓操作系統(tǒng)的資源充分利用,使用多任務處理的方式讓CPU在處理任務時輪流執(zhí)行多個線程,從而充分利用了CPU的時間,提高了系統(tǒng)的資源利用率。
3. 優(yōu)化用戶體驗
多線程技術可以讓程序在執(zhí)行過程中保持響應,同時也保證了線程之間的相互獨立。這使得程序不會因為某個線程的執(zhí)行出現(xiàn)問題而導致整個程序崩潰,從而提升了用戶的體驗。
二、Linux下的多線程實現(xiàn)
1. 線程的創(chuàng)建和結束
在Linux下,線程的創(chuàng)建和結束是通過調用線程庫函數(shù)來實現(xiàn)的。pthread_create()用于創(chuàng)建線程,該函數(shù)原型如下:
“`
#include
extern int pthread_create(pthread_t* tid, const pthread_attr_t* attr, void (*fn)(void*), void* arg);
“`
其中,tid是指向線程標識符的指針,attr是指向線程屬性的指針,fn是指向線程函數(shù)的指針,arg是傳遞給線程函數(shù)的參數(shù),該函數(shù)執(zhí)行成功時返回0,否則返回錯誤碼。
pthread_exit()用于線程的結束,該函數(shù)原型如下:
“`
#include
extern void pthread_exit(void* retVal);
“`
其中,retVal是線程返回的參數(shù),該函數(shù)不返回值,直接退出線程。
2. 線程同步
線程同步是為了保證多個線程之間的有序執(zhí)行,以避免由于多個線程并發(fā)執(zhí)行而導致的問題。Linux下提供了多個方法來實現(xiàn)線程同步,如:互斥鎖(mutex)、條件變量(condition variable)等。
pthread_mutex_t用于互斥鎖的創(chuàng)建,該函數(shù)原型如下:
“`
#include
extern int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr);
“`
其中,mutex是指向互斥鎖變量的指針,attr是指向互斥鎖屬性變量的指針。該函數(shù)執(zhí)行成功返回0,否則返回錯誤碼。pthread_mutex_lock()用于互斥鎖的加鎖,該函數(shù)原型如下:
“`
#include
extern int pthread_mutex_lock(pthread_mutex_t* mutex);
“`
其中,mutex是互斥鎖變量。pthread_mutex_unlock()用于互斥鎖的解鎖,該函數(shù)原型如下:
“`
#include
extern int pthread_mutex_unlock(pthread_mutex_t* mutex);
“`
其中,mutex是互斥鎖變量。
3. 線程池
線程池是一種線程復用的技術,它可以復用一組線程來處理多個任務。線程池中的線程數(shù)量可配置,在需要執(zhí)行任務時,從線程池中取出空閑的線程來處理任務。線程池技術在多線程應用中廣泛應用,可以有效降低因創(chuàng)建線程帶來的開銷,提升程序的執(zhí)行效率。
Linux下提供了線程池的實現(xiàn)pthreadpool,該線程池庫提供了線程池的創(chuàng)建、銷毀、任務的添加等操作,可以方便地實現(xiàn)線程池的使用。
三、多線程編程的實踐
1. 線程函數(shù)的實現(xiàn)
線程函數(shù)是一個重要的部分,是執(zhí)行多線程任務的核心代碼,下面通過一個簡單的例子來說明線程函數(shù)的實現(xiàn)方式。
“`
#include
#include
void* thread_fun(void* arg)
{
printf(“thread start…\n”);
printf(“thread end…\n”);
pthread_exit(NULL);
}
int mn(int argc, char** argv)
{
pthread_t tid;
pthread_create(&tid, NULL, thread_fun, NULL);
pthread_join(tid, NULL);
return 0;
}
“`
在上面的代碼中,我們使用pthread_create()函數(shù)創(chuàng)建了一個線程,指定了線程函數(shù)thread_fun()作為線程的入口點。該線程函數(shù)只是簡單地打印了一些信息,并最終使用pthread_exit()函數(shù)退出線程。
2. 線程同步的實現(xiàn)
線程同步是一個重要的概念,它可以實現(xiàn)多個線程之間的有序執(zhí)行。下面通過一個簡單的例子來說明如何使用互斥鎖來實現(xiàn)線程同步。
“`
#include
#include
pthread_mutex_t mutex;
void* thread_fun(void* arg)
{
pthread_mutex_lock(&mutex);
printf(“thread start…\n”);
sleep(2);
printf(“thread end…\n”);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int mn(int argc, char** argv)
{
pthread_t tid[10];
pthread_mutex_init(&mutex, NULL);
int i;
for (i = 0; i
{
pthread_create(&tid[i], NULL, thread_fun, NULL);
}
for (i =0; i
{
pthread_join(tid[i], NULL);
}
pthread_mutex_destroy(&mutex);
return 0;
}
“`
在上面的代碼中,我們使用了pthread_mutex_lock()函數(shù)和pthread_mutex_unlock()函數(shù)來實現(xiàn)線程同步。線程函數(shù)在執(zhí)行的時候會先對互斥鎖加鎖,然后在最后釋放互斥鎖,以此來保證多個線程之間的有序執(zhí)行。
3. 線程池的實現(xiàn)
線程池是一種線程復用的技術,能夠大大提高程序的運行效率。下面通過一個簡單的例子來說明如何使用Linux提供的pthreadpool線程池來實現(xiàn)多線程任務的執(zhí)行。
“`
#include
#include
void task_fun(void* arg)
{
printf(“task start…\n”);
sleep(2);
printf(“task end…\n”);
}
int mn(int argc, char** argv)
{
pthreadpool_t pool;
pthreadpool_create(&pool, 5);
int i;
for (i = 0; i
{
pthreadpool_add(&pool, task_fun, NULL);
}
pthreadpool_wt(&pool);
pthreadpool_destroy(&pool);
return 0;
}
“`
在上面的代碼中,我們創(chuàng)建了一個線程池,設置了5個線程的數(shù)量,然后添加了10個任務到線程池中,最后等待線程池中的所有任務執(zhí)行完成,這里我們使用了pthreadpool_add()函數(shù)和pthreadpool_wt()函數(shù)來實現(xiàn)任務的添加和等待線程池中任務的執(zhí)行。
四、
相關問題拓展閱讀:
- 關于linux下多線程編程
關于linux下多線程編程
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//禁止join, 分離的線程對象
//線拿亮腔程…前進鍵帶吧….
if(0 == pthread_create( &(thread->id), &attr, wrapper_fn, thread )){//wrapper_fn 是函數(shù)指針 thread是函數(shù)參數(shù)
pool->tp_total++; //池中的已裝入線程總數(shù)數(shù)+1
}else{
ret = -1;
printf(“Can’t create thread\n”);
pthread_mutex_destroy( &(thread->mutex));
pthread_cond_destroy( &(thread->cond));
free(thread);
}
估消衫計是你沒有處理join, 我這個線程池封了好久了. 所以對pthread沒什么印象了
pthread_join 線程停止等待函數(shù)沒有調用
pthread_create 線程生成后,沒有等子線程停止,主線程就先停止了。
主線程停止后,整個程序停止,子線程在沒有printf的時候就被結束宏旅納了。
結論:不是你沒有看到結果,而是在鎮(zhèn)凳子線蔽沒程printf(“………………\n”);之前整個程序就已經(jīng)停止了。
#include
#include
#include
#include
#include
#include
#define FALSE -1
#define TRUE 0
void *shuchu( void *dumy )
{
int j;
printf(“………………\n”);
}
int main()
{
int i = 0;
int rc = 0;
int ret1;
pthread_t p_thread1;
if(0!=(ret1 = pthread_create(&p_thread1, NULL, shuchu, NULL)))printf(“sfdfsdfi\n”);
printf(“\n”,p_thread1);
pthread_join(p_thread1, NULL);
return TRUE;
}
main()方法怎么回返回int型值?
而且也看不出多線程在哪????
成都網(wǎng)站營銷推廣找創(chuàng)新互聯(lián),全國分站站群網(wǎng)站搭建更好做SEO營銷。
創(chuàng)新互聯(lián)(www.cdcxhl.com)四川成都IDC基礎服務商,價格厚道。提供成都服務器托管租用、綿陽服務器租用托管、重慶服務器托管租用、貴陽服務器機房服務器托管租用。
網(wǎng)站標題:Linux下的多線程實現(xiàn)—提高程序運行效率 (linux 多線程實現(xiàn))
標題路徑:http://fisionsoft.com.cn/article/coessis.html


咨詢
建站咨詢
