共計 4403 個字符,預計需要花費 12 分鐘才能閱讀完成。
這篇“Linux 多線程編程實例代碼分析”文章的知識點大部分人都不太理解,所以丸趣 TV 小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Linux 多線程編程實例代碼分析”文章吧。
下面先來一個實例。我們通過創建兩個線程來實現對一個數的遞加。或許這個實例沒有實際運用的價值,但是稍微改動一下,我們就可以用到其他地方去拉。
代碼:
/*thread_example.c : c multiple thread programming in linux
*author : falcon
*e-mail : tunzhj03@st.lzu.edu.cn
*/
#include pthread.h
#include stdio.h
#include sys/time.h
#include string.h
#define max 10
pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;
void *thread1()
printf ( thread1 : i m thread 1/n
for (i = 0; i max; i++)
{ printf( thread1 : number = %d/n ,number);
pthread_mutex_lock(mut);
number++;
pthread_mutex_unlock(mut);
sleep(2);
}
printf(thread2 : number = %d/n ,number);
pthread_mutex_lock(mut);
number++;
pthread_mutex_unlock(mut);
sleep(3);
}
int temp;
memset(thread, 0, sizeof(thread)); //comment1
/* 創建線程 */
if((temp = pthread_create( thread[0], null, thread1, null)) != 0) //comment2
printf( 線程 1 創建失敗!/n
else
printf( 線程 1 被創建 /n
if((temp = pthread_create( thread[1], null, thread2, null)) != 0) //comment3
printf( 線程 2 創建失敗
else
printf( 線程 2 被創建 /n
void thread_wait(void)
/* 等待線程結束 */
if(thread[0] !=0) { //comment4 pthread_join(thread[0],null);
printf( 線程 1 已經結束 /n
}
if(thread[1] !=0) { //comment5 pthread_join(thread[1],null);
printf( 線程 2 已經結束 /n
}
int main()
/* 用默認屬性初始化互斥鎖 */
pthread_mutex_init(mut,null);
printf( 我是主函數哦,我正在創建線程,呵呵 /n
thread_create();
printf( 我是主函數哦,我正在等待線程完成任務阿,呵呵 /n
thread_wait();
return 0;
}
下面我們先來編譯、執行一下
引文:
falcon@falcon:~/program/c/code/ftp$ gcc -lpthread -o thread_example thread_example.c
falcon@falcon:~/program/c/code/ftp$ ./thread_example
我是主函數哦,我正在創建線程,呵呵
線程 1 被創建
線程 2 被創建
我是主函數哦,我正在等待線程完成任務阿,呵呵
thread1 : i m thread 1
thread1 : number = 0
thread2 : i m thread 2
thread2 : number = 1
thread1 : number = 2
thread2 : number = 3
thread1 : number = 4
thread2 : number = 5
thread1 : number = 6
thread1 : number = 7
thread2 : number = 8
thread1 : number = 9
thread2 : number = 10
thread1 : 主函數在等我完成任務嗎?線程 1 已經結束
thread2 : 主函數在等我完成任務嗎?線程 2 已經結束
實例代碼里頭的注釋應該比較清楚了吧,下面我把網路上介紹上面涉及到的幾個函數和變量給引用過來。
引文:
線程相關操作
一 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 處時,如果該鎖此時被另一個線程使用,那此線程被阻塞,即程序將等待到另一個線程釋放此互斥鎖。
注意:
1 需要說明的是,上面的兩處 sleep 不光是為了演示的需要,也是為了讓線程睡眠一段時間,讓線程釋放互斥鎖,等待另一個線程使用此鎖。下面的參考資料 1 里頭說明了該問題。但是在 linux 下好像沒有 pthread_delay_np 那個函數(我試了一下,提示沒有定義該函數的引用),所以我用了 sleep 來代替,不過參考資料 2 中給出另一種方法,好像是通過 pthread_cond_timedwait 來代替,里頭給出了一種實現的辦法。
2 請千萬要注意里頭的注釋 comment1-5,那是我花了幾個小時才找出的問題所在。
如果沒有 comment1 和 comment4,comment5, 將導致在 pthread_join 的時候出現段錯誤,另外,上面的 comment2 和 comment3 是根源所在,所以千萬要記得寫全代碼。因為上面的線程可能沒有創建成功,導致下面不可能等到那個線程結束,而在用 pthread_join 的時候出現段錯誤(訪問了未知的內存區)。另外,在使用 memset 的時候,需要包含 string.h 頭文件哦
以上就是關于“Linux 多線程編程實例代碼分析”這篇文章的內容,相信大家都有了一定的了解,希望丸趣 TV 小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注丸趣 TV 行業資訊頻道。