/*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("thread1 :主函數在等我完成任務嗎?\n");
pthread_exit(NULL);
}
void *thread2()
{
printf("thread2 : I'm thread 2\n");
for (i = 0; i < MAX; i++)
{
printf("thread2 : number = %d\n",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(3);
}
printf("thread2 :主函數在等我完成任務嗎?\n");
pthread_exit(NULL);
}
void thread_create(void)
{
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處時,若是該鎖此時被另外一個線程使用,那此線程被阻塞,即程序將等待到另外一個線程釋放此互斥鎖。
出處:http://zhuwenlong.blog.51cto.com/209020/40339php