linux下c語言多執行緒程式設計案例

2021-07-02 13:20:39 字數 3475 閱讀 7368

最近在學習linux核心程式設計,由於linux核心完全是c**,所以想深入研究下c,今天就弄了下c下的多執行緒

#include

#include

#include

#include

#define max 10

pthread_t thread[2];  //宣告兩個執行緒識別符號

pthread_mutex_t mut;  //宣告鎖

int number=0,i;    //number 為全域性共享變數

void *thread1()}

下邊編譯執行下:

chenmeng@chenmeng-virtualbox:~/chenfile/ccode/thread$ gcc -c  thread_test.c

chenmeng@chenmeng-virtualbox:~/chenfile/ccode/thread$ gcc -o test thread_tes

t.o -lpthread

chenmeng@chenmeng-virtualbox:~/chenfile/ccode/thread$ ls

test

thread_test.c  thread_test.o

chenmeng@chenmeng-virtualbox:~/chenfile/ccode/thread$ ./test

i am the main function,i am creating thread.

thread 1 create success!

thread 2 create success!

i am the main function, i am waiting thread finished!

thread2 : i am thread 2

thread2: number = 0

thread1 : i am thread 1

thread 1: number = 1

thread2: number = 2

thread 1: number = 3

thread2: number = 4

thread 1: number = 5

thread2: number = 6

thread 1: number = 7

thread2: number = 8

thread 1: number = 9

thread2: number = 10

thread 1 has finished!

thread 2 has finished!

從結果就可以看到兩個執行緒同時執行,並且在鎖的保護下,變數訪問正常!

執行緒相關操作

一 pthread_t

pthread_t在標頭檔案/usr/include/bits/pthreadtypes.h中定義:

typedef unsigned long int pthread_t;

它是乙個執行緒的識別符號。

二 pthread_create

函式pthread_create用來建立乙個執行緒,它的原型為:

extern int pthread_create __p ((pthread_t *__thread, __const pthread_attr_t *__attr,

void *(*__start_routine) (void *), void *__arg));

第乙個引數為指向執行緒識別符號的指標,第二個引數用來設定執行緒屬性,第三個引數是執行緒執行函式的起始位址,最後乙個引數是執行函式的引數。這裡,我們的函式thread不需要引數,所以最後乙個引數設為空指標。第二個引數我們也設為空指標,這樣將生成預設屬性的執行緒。對執行緒屬性的設定和修改我們將在下一節闡述。當建立執行緒成功時,函式返回0,若不為0則說明建立執行緒失敗,常見的錯誤返回**為eagain和einval。前者表示系統限制建立新的執行緒,例如執行緒數目過多了;後者表示第二個引數代表的執行緒屬性值非法。建立執行緒成功後,新建立的執行緒則執行引數三和引數四確定的函式,原來的執行緒則繼續執行下一行**。

三 pthread_join pthread_exit

函式pthread_join用來等待乙個執行緒的結束。函式原型為:

extern int pthread_join __p ((pthread_t __th, void **__thread_return));

第乙個引數為被等待的執行緒識別符號,第二個引數為乙個使用者定義的指標,它可以用來儲存被等待執行緒的返回值。這個函式是乙個執行緒阻塞的函式,呼叫它的函式將一直等待到被等待的執行緒結束為止,當函式返回時,被等待執行緒的資源被收回。乙個執行緒的結束有兩種途徑,一種是象我們上面的例子一樣,函式結束了,呼叫它的執行緒也就結束了;另一種方式是通過函式pthread_exit來實現。它的函式原型為:

extern void pthread_exit __p ((void *__retval)) __attribute__ ((__noreturn__));

唯一的引數是函式的返回**,只要pthread_join中的第二個引數thread_return不是null,這個值將被傳遞給 thread_return。最後要說明的是,乙個執行緒不能被多個執行緒等待,否則第乙個接收到訊號的執行緒成功返回,其餘呼叫pthread_join的執行緒則返回錯誤**esrch。

在這一節裡,我們編寫了乙個最簡單的執行緒,並掌握了最常用的三個函式pthread_create,pthread_join和pthread_exit。下面,我們來了解執行緒的一些常用屬性以及如何設定這些屬性。

互斥鎖相關

互斥鎖用來保證一段時間內只有乙個執行緒在執行一段**。

一 pthread_mutex_init

函式pthread_mutex_init用來生成乙個互斥鎖。null引數表明使用預設屬性。如果需要宣告特定屬性的互斥鎖,須呼叫函式 pthread_mutexattr_init。函式pthread_mutexattr_setpshared和函式 pthread_mutexattr_settype用來設定互斥鎖屬性。前乙個函式設定屬性pshared,它有兩個取值, pthread_process_private和pthread_process_shared。前者用來不同程序中的執行緒同步,後者用於同步本程序的不同執行緒。在上面的例子中,我們使用的是預設屬性pthread_process_ private。後者用來設定互斥鎖型別,可選的型別有pthread_mutex_normal、pthread_mutex_errorcheck、 pthread_mutex_recursive和pthread _mutex_default。它們分別定義了不同的上所、解鎖機制,一般情況下,選用最後乙個預設屬性。

二 pthread_mutex_lock pthread_mutex_unlock pthread_delay_np

pthread_mutex_lock宣告開始用互斥鎖上鎖,此後的**直至呼叫pthread_mutex_unlock為止,均被上鎖,即同一時間只能被乙個執行緒呼叫執行。當乙個執行緒執行到pthread_mutex_lock處時,如果該鎖此時被另乙個執行緒使用,那此執行緒被阻塞,即程式將等待到另乙個執行緒釋放此互斥鎖。

希望大家共同學習,共同進步!

linux下C語言多執行緒程式設計

include include include include define max 10pthread t thread 2 pthread mutex t mut int number 0 i void thread1 printf thread1 主函式在等我完成任務嗎?n pthread e...

多執行緒程式設計 c語言linux下

適用與linux系統 1.了解基本概念 程序 是計算機所執行的乙個任務的描述,是面向作業系統的最小單位,作業系統能執行很多程序 執行自己寫的乙份 程式,就是讓作業系統執行乙個自己程式的程序 作業系統會根據程式分配定量的資源 執行緒 面想程式 程序 的,把乙個程式分成多個執行緒可以實現並髮式,多工執行...

linux 下c語言 多執行緒程式設計

最近學習c語言和linux,記錄一下linux中線程的簡單使用 linux執行緒一般用pthread庫建立。pthread是 posix thread的簡稱。在linux的 lib目錄下,可以找到名為libpthread x.x.so x.x是版本號 的庫。下面模擬兩個執行緒 threaddemo....