LINUX 多執行緒C語音程式設計

2021-08-21 06:14:33 字數 3442 閱讀 5527

1.使用多執行緒需要包含標頭檔案

#include
2.建立執行緒執行的函式

void *func(void *arg)// *arg傳參

3.建立執行緒
#include int pthread_create(pthread_t * tidp, const pthread_attr_t *attr, void *(*start_rtn)(void *), void *arg);
tid  : 執行緒tid

attr: 執行緒的屬性,null 使用預設的屬性,如下可構造乙個屬性結構體

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr, pthread_create_joinable);

pthread_attr_setscope(&attr, pthread_scope_system);

srart_rtn :執行緒執行的主題函式

arg :外部傳給srart_rtn的引數

注意:attr使用完後記得執行 pthread_attr_destroy(&attr);

4.執行緒退出

4.1執行緒執行完後隱式退出

4.2由執行緒本顯示呼叫pthread_exit函式退出

pthread_exit(void *retval)
4.3其他程式呼叫pthtead_cancel(指定pid)

pthread_canel(pthread_t thread)
5.乙個執行緒等待另乙個執行緒退出

吐過乙個執行緒要等待另乙個執行緒的終止,可以使用pthread_join(),該函式的作用是呼叫pthread_join的執行緒將被掛起直到等待的執行緒終止退出

6.執行緒互斥

int aaa;//多執行緒共享的全域性變數

pthread_mutex_t mutex;//互斥量,這個應該也是全域性變數

pthread_mutex_init(&mutex,null);//按預設屬性初始化互斥變數

//對全域性變數aaa上鎖

pthread_mutex_lock(&mutex);

...對aaa的操作

pthread_mutex_unlock(&mutex);

7.執行緒同步

同步是指乙個執行緒等待某件事情的發生,只有當等待的事件發生了執行緒才繼續執行,否則執行緒一直掛起並放棄cpu,

pthread_cond_init (pthread_cond_t *cond, const pthread_condattr_t *attr);
等待條件發生
pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
廣播條件已經發生

pthread_cond_broadcast (pthread_cond_t *cond) ;
解除某個等待執行緒

pthread_cond_signal (pthread_cond_t *cond) ;
釋放條件變數資源

pthread_cond_destroy
例子

pthread_cond_t  cond_audio_start;//

pthread_mutex_t audio_mutex;//

pthread_mutex_init(&audio_mutex, null);//初始化

pthread_cond_init(&cond_audio_start, null);//初始化

//等待條件發生

pthread_cond_wait(&cond_audio_start, &audio_mutex);

pthread_mutex_unlock(&audio_mutex);//釋放

//使用完 **資源

pthread_mutex_destroy(&audio_mutex);

pthread_cond_destroy(&cond_audio_start);

下面我們還是以名的生產者/消費者問題為例來闡述linux執行緒的控制和通訊。一組生產者執行緒與一組消費者執行緒通過緩衝區發生聯絡。生產者執行緒將生產的產品送入緩衝區,消費者執行緒則從中取出產品。緩衝區有n 個,是乙個環形的緩衝池。 

#include #include #define buffer_size 16 // 緩衝區數量

struct prodcons

;/* 初始化緩衝區結構 */

void init(struct prodcons *b)

/* 將產品放入緩衝區,這裡是存入乙個整數*/

void put(struct prodcons *b, int data)

/* 寫資料,並移動指標 */

b->buffer[b->writepos] = data;

b->writepos++;

if (b->writepos >= buffer_size)

b->writepos = 0;

/* 設定緩衝區非空的條件變數*/

pthread_cond_signal(&b->notempty);

pthread_mutex_unlock(&b->lock);

} /* 從緩衝區中取出整數*/

int get(struct prodcons *b)

/* 讀資料,移動讀指標*/

data = b->buffer[b->readpos];

b->readpos++;

if (b->readpos >= buffer_size)

b->readpos = 0;

/* 設定緩衝區未滿的條件變數*/

pthread_cond_signal(&b->notfull);

pthread_mutex_unlock(&b->lock);

return data;}

/* 測試:生產者執行緒將1 到10000 的整數送入緩衝區,消費者線

程從緩衝區中獲取整數,兩者都列印資訊*/

#define over ( - 1)

struct prodcons buffer;

void *producer(void *data)

put(&buffer, over);

return null;}

void *consumer(void *data)

return null;}

int main(void)

注意:編譯是加-lpthread 選項

Linux 多執行緒程式設計

1.建立執行緒和退出的函式原型 int pthread create pthread t thread,pthread attr t attr,void start routine void void arg pthread exit 0 其他還有很多相關的函式。2.編譯時要加上 lpthread ...

Linux多執行緒程式設計

linux 多執行緒程式設計 多執行緒支援 posix 執行緒介面,稱為 pthread,pthread create 用來建立執行緒,pthread join 等待執行緒結束,函式的原型分別如下 extern int pthread create p pthread t thread,const ...

linux 多執行緒程式設計

多執行緒的使用 典型的執行緒包括乙個執行時間系統,它可以按透明的方式來管理執行緒。通常執行緒包包括對執行緒的建立和刪除,以及對互斥和條件變數的呼叫。posix標準執行緒庫具有這些呼叫。這些包還提供執行緒的動態建立和刪除,因此,直到執行時間之前,執行緒的個數不必知道。執行緒具有乙個id 乙個堆疊 乙個...