linux執行緒之間的通訊

2021-07-13 11:41:27 字數 4773 閱讀 8951

執行緒之間的通訊:

1、  互斥量

用的api函式有:pthread_mutex_init、pthread_mutex_lock、pthread_mutex_unlock、pthread_mutex_trylock

示例:

//thread_mutex.cpp

#include #include#include #include #include #includeusing namespace std;

typedef unsigned int u32;

typedef unsigned char byte;

bool flag_exit = false;

char *data ="test pthread mutex!";

typedef struct job

job,*pjob;

queueg_thrd_queue;

pthread_mutex_t mutex = pthread_mutex_initializer;

typedef void (*sighandler_t)(int);

void handlesignal(int signal)

pjob packjob(u32 id,byte *pdata,u32 len )

return pret;

}typedef void* (*pthrd_fun)(void*);

void * thrd_function_1(void *parg)

cout << "thrd id is: " << pthread_self() << " exit!" << endl;

iexit = 0;

pthread_exit(&iexit);

}void * thrd_function_2(void *parg)

cout << "thrd id is: " << pthread_self() << " exit!" << endl;

iexit = 0;

pthread_exit(&iexit);

}int main(int argc,char *argv)

iret = pthread_create(&thread_id2,null,thrd_function_2,null);

if(iret != 0)

//等待子執行緒的退出

pthread_join(thread_id1,null);

pthread_join(thread_id2,null);

//釋放記憶體

while(!g_thrd_queue.empty())

cout << "main thread is exit!" << endl;

return iret;

}

注意:有時候我們需要檢測乙個互斥體的狀態卻不希望被阻塞,那麼我們就可以pthread_mutex_trylock。當你對乙個解鎖狀態的互斥體呼叫pthread_mutex_trylock 時,就如呼叫pthread_mutex_lock 一樣會鎖定這個互斥體;pthread_mutex_trylock 會返回 0。 而當互斥體已經被其它執行緒鎖定的時候,pthread_mutex_trylock 不會阻塞。相應的,pthread_mutex_trylock 會返回錯誤碼ebusy。持有鎖的其它執行緒不會受到影響。你可以稍後再次嘗試鎖定這個互斥體

2、 訊號量

用的api函式有:sem_init、sem_wait、sem_post、sem_destory、sem_trywait

示例:

#include#include//thread_sem.cc

#include#include#include#include#includeusing namespace std;

typedef unsigned int u32;

typedef unsigned char byte;

bool flag_exit = false;

char *data ="test pthread mutex!";

typedef struct job

job,*pjob;

queueg_thrd_queue;

pthread_mutex_t mutex = pthread_mutex_initializer;

sem_t sem_job_count;

typedef void (*sighandler_t)(int);

void handlesignal(int signal)

pjob packjob(u32 id,byte *pdata,u32 len )

return pret;

}typedef void* (*pthrd_fun)(void*);

void * thrd_function_1(void *parg)

cout << "thrd id is: " << pthread_self() << " exit!" << endl;

iexit = 0;

pthread_exit(&iexit);

}void * thrd_function_2(void *parg)

cout << "thrd id is: " << pthread_self() << " exit!" << endl;

iexit = 0;

pthread_exit(&iexit);

}int main(int argc,char *argv)

iret = pthread_create(&thread_id2,null,thrd_function_2,null);

if(iret != 0)

//等待子執行緒的退出

pthread_join(thread_id1,null);

pthread_join(thread_id2,null);

//釋放記憶體

while(!g_thrd_queue.empty())

cout << "main thread is exit!" << endl;

return iret;

}

注意:sem_trywait和pthread_mutex_trylock用法類似。

3、  條件變數

用的api函式有:pthread_cond_init、用pthread_cond_signal、pthread_cond_wait、,pthread_cond_broadcast、pthread_cond_destroy

示例:

//thread_con.cc

#include#include#include#include#include#include#includeusing namespace std;

typedef unsigned int u32;

typedef unsigned char byte;

bool flag_exit = false;

char *data ="test pthread mutex!";

typedef struct job

job,*pjob;

queueg_thrd_queue;

pthread_mutex_t mutex = pthread_mutex_initializer;

pthread_cond_t g_thread_flag_cv;

typedef void (*sighandler_t)(int);

void handlesignal(int signal)

pjob packjob(u32 id,byte *pdata,u32 len )

return pret;

}typedef void* (*pthrd_fun)(void*);

void * thrd_function_1(void *parg)

flag_exit = true;

//觸發條件變數

pthread_cond_signal(&g_thread_flag_cv);

//解除互斥體

pthread_mutex_unlock(&mutex);

cout << "thrd id is: " << pthread_self() << " exit!" << endl;

iexit = 0;

pthread_exit(&iexit);

}void * thrd_function_2(void *parg)

//解除互斥體

pthread_mutex_unlock(&mutex);

cout << "thrd id is: " << pthread_self() << " exit!" << endl;

iexit = 0;

pthread_exit(&iexit);

}int main(int argc,char *argv)

iret = pthread_create(&thread_id2,null,thrd_function_2,null);

if(iret != 0)

//等待子執行緒的退出

pthread_join(thread_id1,null);

pthread_join(thread_id2,null);

//釋放記憶體

while(!g_thrd_queue.empty())

pthread_cond_destroy(&g_thread_flag_cv);

cout << "main thread is exit!" << endl;

return iret;

}

注意:pthread_cond_broadcast 函式會將所有等待該條件變數的執行緒解鎖而不是

僅僅解鎖乙個執行緒。

執行緒之間的通訊

class resclass inthread extends thread override public void run else count count 1 2 class outthread extends thread override public void run 資料發生錯亂,造成...

程序之間 執行緒之間的通訊方式

1 程序間的8中通訊方式 1 無名管道 pipe 管道是一種半雙工的通訊方式,資料只能單向流動,而且只能在具有親緣關係的程序間使用。程序的親緣關係通常是指父子程序關係。2 2 高階管道 popen 將另乙個程式當做乙個新的程序在當前程式程序中啟動,則它算是當前程式的子程序,這種方式我們成為高階管道方...

多執行緒之間通訊

多執行緒之間通訊,其實就是多個執行緒在操作同乙個資源,但是操作的動作不同。需求 第乙個執行緒寫入 input 使用者,另乙個執行緒取讀取 out 使用者.實現讀乙個,寫乙個操作。共享資源源實體類 class res輸入執行緒資源 class intthrad extends thread overr...