Linux多線程編程(一)
一、什么是線程?
線程是進(jìn)程的一個(gè)實(shí)體,是CPU調(diào)度和分派的基本單位,它是比進(jìn)程更小的能獨(dú)立運(yùn)行的基本單位。線程自己基本上不擁有系統(tǒng)資源,只擁有一點(diǎn)在運(yùn)行中必不可少的資源(如程序計(jì)數(shù)器,一組寄存器和棧),但是它可與同屬一個(gè)進(jìn)程的其他的線程共享進(jìn)程所擁有的全部資源。
二、什么時(shí)候使用多線程? 當(dāng)多個(gè)任務(wù)可以并行執(zhí)行時(shí),可以為每個(gè)任務(wù)啟動(dòng)一個(gè)線程。
三、線程的創(chuàng)建 使用pthread_create函數(shù)。
#include<pthread.h> int pthread_create (pthread_t *__restrict __newthread,//新創(chuàng)建的線程ID __const pthread_attr_t *__restrict __attr,//線程屬性 void *(*__start_routine) (void *),//新創(chuàng)建的線程從start_routine開始執(zhí)行 void *__restrict __arg)//執(zhí)行函數(shù)的參數(shù)
返回值:成功-0,失敗-返回錯(cuò)誤編號(hào),可以用strerror(errno)函數(shù)得到錯(cuò)誤信息
四、線程的終止 三種方式線程從執(zhí)行函數(shù)返回,返回值是線程的退出碼線程被同一進(jìn)程的其他線程取消調(diào)用pthread_exit()函數(shù)退出。這里不是調(diào)用exit,因?yàn)榫€程調(diào)用exit函數(shù),會(huì)導(dǎo)致線程所在的進(jìn)程退出。
一個(gè)小例子:
啟動(dòng)兩個(gè)線程,一個(gè)線程對(duì)全局變量num執(zhí)行加1操作,執(zhí)行五百次,一個(gè)線程對(duì)全局變量執(zhí)行減1操作,同樣執(zhí)行五百次。
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <string.h> int num=0; void *add(void *arg) {//線程執(zhí)行函數(shù),執(zhí)行500次加法 int i = 0,tmp; for (; i <500; i++) { tmp=num+1; num=tmp; printf("add+1,result is:%d\n",num); } return ((void *)0); } void *sub(void *arg)//線程執(zhí)行函數(shù),執(zhí)行500次減法 { int i=0,tmp; for(;i<500;i++) { tmp=num-1; num=tmp; printf("sub-1,result is:%d\n",num); } return ((void *)0); } int main(int argc, char** argv) { pthread_t tid1,tid2; int err; void *tret; err=pthread_create(&tid1,NULL,add,NULL);//創(chuàng)建線程 if(err!=0) { printf("pthread_create error:%s\n",strerror(err)); exit(-1); } err=pthread_create(&tid2,NULL,sub,NULL); if(err!=0) { printf("pthread_create error:%s\n",strerror(err)); exit(-1); } err=pthread_join(tid1,&tret);//阻塞等待線程id為tid1的線程,直到該線程退出 if(err!=0) { printf("can not join with thread1:%s\n",strerror(err)); exit(-1); } printf("thread 1 exit code %d\n",(int)tret); err=pthread_join(tid2,&tret); if(err!=0) { printf("can not join with thread1:%s\n",strerror(err)); exit(-1); } printf("thread 2 exit code %d\n",(int)tret); return 0; }
使用g++編譯該文件(g++ main.cpp -o main)。此時(shí)會(huì)報(bào)錯(cuò)undefined reference to `pthread_create'。
報(bào)這個(gè)錯(cuò)誤的原因是:pthread庫不是linux默認(rèn)的庫,所以在編譯時(shí)候需要指明libpthread.a庫。
解決方法:在編譯時(shí),加上-lpthread參數(shù)。
執(zhí)行結(jié)果:
乍一看,結(jié)果是對(duì)的,加500次,減500次,最后結(jié)果為0。但是仔細(xì)看所有的輸出,你會(huì)發(fā)現(xiàn)有異樣的東西。
導(dǎo)致這個(gè)不和諧出現(xiàn)的原因是,兩個(gè)線程可以對(duì)同一變量進(jìn)行修改。假如線程1執(zhí)行tmp=50+1后,被系統(tǒng)中斷,此時(shí)線程2對(duì)num=50執(zhí)行了減一操作,當(dāng)線程1恢復(fù),在執(zhí)行num=tmp=51。而正確結(jié)果應(yīng)為50。所以當(dāng)多個(gè)線程對(duì)共享區(qū)域進(jìn)行修改時(shí),應(yīng)該采用同步的方式。
五、線程同步線程同步的三種方式:
1、互斥量 互斥量用pthread_mutex_t數(shù)據(jù)類型來表示。
兩種方式初始化,第一種:賦值為常量PTHREAD_MUTEX_INITIALIZER;第二種,當(dāng)互斥量為動(dòng)態(tài)分配是,使用pthread_mutex_init函數(shù)進(jìn)行初始化,使用pthread_mutex_destroy函數(shù)銷毀。
#include<pthread.h> int pthread_mutex_init (pthread_mutex_t *__mutex, __const pthread_mutexattr_t *__mutexattr); int pthread_mutex_destroy (pthread_mutex_t *__mutex);
返回值:成功-0,失敗-錯(cuò)誤編號(hào) 加解鎖加鎖調(diào)用pthread_mutex_lock,解鎖調(diào)用pthread_mutex_unlock。
#include<pthread.h> int pthread_mutex_lock (pthread_mutex_t *__mutex); int pthread_mutex_unlock (pthread_mutex_t *__mutex);
使用互斥量修改上一個(gè)程序(修改部分用紅色標(biāo)出):
pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER; void *add(void *arg) { int i = 0,tmp; for (; i <500; i++) { pthread_mutex_lock(&mylock); tmp=num+1; num=tmp; printf("+1,result is:%d\n",num); pthread_mutex_unlock(&mylock); } return ((void *)0); } void *sub(void *arg) { int i=0,tmp; for(;i<500;i++) { pthread_mutex_lock(&mylock); tmp=num-1; num=tmp; printf("-1,result is:%d\n",num); pthread_mutex_unlock(&mylock); } return ((void *)0); }
2、讀寫鎖 允許多個(gè)線程同時(shí)讀,只能有一個(gè)線程同時(shí)寫。適用于讀的次數(shù)遠(yuǎn)大于寫的情況。 讀寫鎖初始化:
#include<pthread.h> int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock, __const pthread_rwlockattr_t *__restrict __attr); int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
返回值:成功--0,失敗-錯(cuò)誤編號(hào)
加鎖,這里分為讀加鎖和寫加鎖。
讀加鎖:
int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock)
寫加鎖:
int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock)
解鎖用同一個(gè)函數(shù):
int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock)
3、條件變量條件變量用pthread_cond_t數(shù)據(jù)類型表示。條件變量本身由互斥量保護(hù),所以在改變條件狀態(tài)前必須鎖住互斥量。
條件變量初始化:
第一種,賦值常量PTHREAD_COND_INITIALIZER;第二種,使用pthread_cond_init函數(shù)
int pthread_cond_init (pthread_cond_t *__restrict __cond, __const pthread_condattr_t *__restrict __cond_attr);int pthread_cond_destroy (pthread_cond_t *__cond);
條件等待
使用pthread_cond_wait等待條件為真。
pthread_cond_wait (pthread_cond_t *__restrict __cond, pthread_mutex_t *__restrict __mutex)
這里需要注意的是,調(diào)用pthread_cond_wait傳遞的互斥量已鎖定,pthread_cond_wait將調(diào)用線程放入等待條件的線程列表,然后釋放互斥量,在pthread_cond_wait返回時(shí),再次鎖定互斥量。
喚醒線程
pthread_cond_signal喚醒等待該條件的某個(gè)線程,pthread_cond_broadcast喚醒等待該條件的所有線程。
int pthread_cond_signal (pthread_cond_t *__cond); int pthread_cond_broadcast (pthread_cond_t *__cond)
來一個(gè)例子,主線程啟動(dòng)4個(gè)線程,每個(gè)線程有一個(gè)參數(shù)i(i=生成順序),無論線程的啟動(dòng)順序如何,執(zhí)行順序只能為,線程0、線程1、線程2、線程3。
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <string.h> #define DEBUG 1 int num=0; pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t qready=PTHREAD_COND_INITIALIZER; void * thread_func(void *arg) { int i=(int)arg; int ret; sleep(5-i);//線程睡眠,然最先生成的線程,最后蘇醒 pthread_mutex_lock(&mylock);//調(diào)用pthread_cond_wait前,必須獲得互斥鎖 while(i!=num) { #ifdef DEBUG printf("thread %d waiting\n",i); #endif ret=pthread_cond_wait(&qready,&mylock);//該函數(shù)把線程放入等待條件的線程列表,然后對(duì)互斥鎖進(jìn)行解鎖,這兩部都是原子操作。并且在pthread_cond_wait返回時(shí),互斥量再次鎖住。 if(ret==0) { #ifdef DEBUG printf("thread %d wait success\n",i); #endif }else { #ifdef DEBUG printf("thread %d wait failed:%s\n",i,strerror(ret)); #endif } } printf("thread %d is running \n",i); num++; pthread_mutex_unlock(&mylock);//解鎖 pthread_cond_broadcast(&qready);//喚醒等待該條件的所有線程 return (void *)0; } int main(int argc, char** argv) { int i=0,err; pthread_t tid[4]; void *tret; for(;i<4;i++) { err=pthread_create(&tid[i],NULL,thread_func,(void *)i); if(err!=0) { printf("thread_create error:%s\n",strerror(err)); exit(-1); } } for (i = 0; i < 4; i++) { err = pthread_join(tid[i], &tret); if (err != 0) { printf("can not join with thread %d:%s\n", i,strerror(err)); exit(-1); } } return 0; }
在非DEBUG模式,執(zhí)行結(jié)果如圖所示:
在DEBUG模式,執(zhí)行結(jié)果如圖所示:
在DEBUG模式可以看出,線程3先被喚醒,然后執(zhí)行pthread_cond_wait(輸出thread 3 waiting),此時(shí)在pthread_cond_wait中先解鎖互斥量,然后進(jìn)入等待狀態(tài)。這是thread 2加鎖互斥量成功,進(jìn)入pthread_cond_wait(輸出thread 2 waiting) ,同樣解鎖互斥量,然后進(jìn)入等待狀態(tài)。直到線程0,全局變量與線程參數(shù)i一致,滿足條件,不進(jìn)入條件等待,輸出thread 0 is running。全局變量num執(zhí)行加1操作,解鎖互斥量,然后喚醒所有等待該條件的線程。thread 3 被喚醒,輸出thread 3 wait success。但是不滿足條件,再次執(zhí)行pthread_cond_wait。如此執(zhí)行下去,滿足條件的線程執(zhí)行,不滿足條件的線程等待。
相關(guān)文章
CentOS7 LNMP+phpmyadmin環(huán)境搭建 第一篇虛擬機(jī)及centos7安裝
這篇文章主要介紹了CentOS7 LNMP+phpmyadmin環(huán)境搭建第一篇虛擬機(jī)及centos7安裝教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07CentOS6.5下搭建文件共享服務(wù)Samba的教程
這篇文章主要介紹了CentOS6.5下搭建文件共享服務(wù)(Samba)的教程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10centos6.5 安裝hadoop1.2.1的教程詳解【親測版】
這篇文章主要介紹了centos6.5 安裝hadoop1.2.1的教程詳細(xì),該教程小編親測過,非常不錯(cuò),值得推薦給大家,需要的朋友可以參考下2018-08-08解決Linux中Systemd服務(wù)環(huán)境變量缺失的問題
在Linux系統(tǒng)運(yùn)維中,我們可能會(huì)遇到在使用systemd管理的服務(wù)時(shí)無法獲取系統(tǒng)環(huán)境變量,尤其是PATH變量,這確實(shí)是一個(gè)常見的挑戰(zhàn),因?yàn)閟ystemd啟動(dòng)的服務(wù)通常不會(huì)加載用戶的環(huán)境變量,下面,我們將一起探討解決這一問題的幾種方法,需要的朋友可以參考下2024-01-01