Linux 线程及线程间通信
- 線程
- 1.線程相關(guān)接口函數(shù)
- 1)創(chuàng)建線程
- 2)結(jié)束線程
- 3)等待線程
- 2.線程間通信
線程
每一個(gè)進(jìn)程的地址空間是相互獨(dú)立的
每一個(gè)進(jìn)程都有一個(gè)task_struct任務(wù)結(jié)構(gòu)體
在進(jìn)行進(jìn)程的切換時(shí),需要不斷刷新cache緩存,比較消耗資源;
為了減少cache刷新時(shí)的資源消耗,引入了輕量級(jí)進(jìn)程 – 線程。
線程特點(diǎn):
進(jìn)程被稱為最小的資源分配單位
線程稱為CPU最小任務(wù)調(diào)度單位
線程公共數(shù)據(jù):
- 用戶名、用戶組名
- 靜態(tài)數(shù)據(jù)、全局?jǐn)?shù)據(jù)
- 文件描述符
- 線程私有數(shù)據(jù)
線程私有數(shù)據(jù):
- 線程ID
- PC
- 優(yōu)先級(jí)、狀態(tài)、屬性
- 堆棧
1.線程相關(guān)接口函數(shù)
pthread_create() 創(chuàng)建進(jìn)程 pthread_exit 結(jié)束進(jìn)程 pthread_join 等待進(jìn)程在編譯跟線程操作相關(guān)的程序時(shí),需要鏈接線程庫(kù) 線程庫(kù)庫(kù)名pthread
1)創(chuàng)建線程
#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);返回值:成功返回 0,失敗返回錯(cuò)誤號(hào) 參數(shù):thread: 線程對(duì)象attr: 線程屬性,填NULL表示使用默認(rèn)屬性start_routine: 線程處理函數(shù) arg: 給線程處理函數(shù)start_routine傳參,若線程處理函數(shù)沒有參數(shù),則填NULL; /* 線程創(chuàng)建 傳入多個(gè)參數(shù) 舉例 */ void *func1(void *arg); struct m_arg {int a;char b; }pthread_t thread1; struct m_arg arg; arg.a = 1; arg.b = 'A';int ret = pthread_create(&thead1, NULL, func1, &arg);2)結(jié)束線程
#include <pthread.h> void pthread_exit(void *retval);參數(shù):retval: 線程結(jié)束信息,由pthread_join等待接收,若不想返回信息,填NULL3)等待線程
#include <pthread.h> int pthread_join(pthread_t thread, void **retval); //等待線程一般在主線程中調(diào)用2.線程間通信
線程的通信只需要利用全局變量就可以實(shí)現(xiàn)。
在一個(gè)線程使用全局變量時(shí),有可能其他線程也在訪問該數(shù)據(jù),那么某一線程使用的數(shù)據(jù)就可能遭到破壞。
初始化信號(hào)量 sem_init()
#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); 返回值:成功返回 0, 失敗返回 -1; 參數(shù):sem: 信號(hào)量對(duì)象;pshared: 填0表示用于線程間同步value: 信號(hào)量初始值P操作 sem_wait()
#include <semaphore.h> int sem_wait(sem_t *sem);V操作 sem_post()
#include <semaphore.h> int sem_post(sem_t *sem);通過線程的 同步 和 互斥,能夠達(dá)到數(shù)據(jù)保護(hù)的效果
同步: 多個(gè)線程之間按照事先約定好的順序有先后地完成某個(gè)事件
? (練習(xí):創(chuàng)建兩個(gè)線程,一個(gè)從鍵盤獲取數(shù)據(jù),另一個(gè)打印輸出)
信號(hào)量:是系統(tǒng)中的一種資源,本質(zhì)是一個(gè)非負(fù)整數(shù),信號(hào)量的值等于資源的個(gè)數(shù)。
操作信號(hào)量只能由通過特定函數(shù)接口才能訪問:
-
信號(hào)量的初始化 sem_init();
-
P操作(申請(qǐng)資源) sem_wait();
if(是否有資源) {執(zhí)行后續(xù)代碼;信號(hào)量 - 1; } else {阻塞等待,直到有資源喚醒為止; } -
V操作(釋放資源) sem_post();
信號(hào)量 + 1; if(有等待資源的程序) {喚醒程序; }
互斥: 當(dāng)一個(gè)線程使用公共數(shù)據(jù)時(shí),其他線程都不能訪問該公共數(shù)據(jù)
臨界資源 : 多個(gè)線程能夠共同訪問的數(shù)據(jù)
臨界區(qū) : 涉及到臨界資源的代碼模塊
互斥是使用 互斥鎖 保護(hù)臨界區(qū)
互斥鎖的相關(guān)操作接口函數(shù):
/* 互斥鎖的初始化 pthread_mutex_init() */ #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); 返回值:成功返回 0, 失敗返回 -1; 參數(shù):mutex: 互斥鎖對(duì)象mutexattr: 互斥鎖屬性,填NULL表示默認(rèn)屬性 /* 申請(qǐng)鎖 pthread_mutex_loc() */ #include <pthread.h> int pthread_mutex_lock(pthread_mutex_t *mutex); 參數(shù):mutex: 互斥鎖對(duì)象 /* 釋放鎖 pthread_mutex_unlock() */ #include <pthread.h> int pthread_mutex_unlock(pthread_mutex_t *mutex); 參數(shù):mutex: 互斥鎖對(duì)象總結(jié)
以上是生活随笔為你收集整理的Linux 线程及线程间通信的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EOS与金格iWebOffice集成应用
- 下一篇: linux 卸载vsftpd服务器,vs