linux下 C++如何實現多執行緒
多執行緒是多工處理的一種特殊形式,多工處理允許讓電腦同時執行兩個或兩個以上的程式。一般情況下,兩種型別的多工處理:基於程序和基於執行緒。
-
基於程序的多工處理是程式的併發執行。
-
執行緒的多工處理是同一程式的片段的併發執行。
多執行緒程式包含可以同時執行的兩個或多個部分。這樣的程式中的每個部分稱為一個執行緒,每個執行緒定義了一個單獨的執行路徑。
C++ 不包含多執行緒應用程式的任何內建支援。相反,它完全依賴於作業系統來提供此功能。
本教程假設您使用的是 Linux 作業系統,我們要使用 POSIX 編寫多執行緒 C++ 程式。POSIX Threads 或 Pthreads 提供的 API 可在多種類 Unix POSIX 系統上可用,比如 FreeBSD、NetBSD、GNU/Linux、Mac OS X 和 Solaris。
建立執行緒
下面的程式,我們可以用它來建立一個 POSIX 執行緒:
#include <pthread.h>
pthread_create (thread, attr, start_routine, arg)
在這裡,pthread_create
建立一個新的執行緒,並讓它可執行。下面是關於引數的說明:
引數說明
引數 | 說明 |
---|---|
thread | 指向執行緒識別符號指標。 |
attr | 一個不透明的屬性物件,可以被用來設定執行緒屬性。您可以指定執行緒屬性物件,也可以使用預設值 NULL。 |
start_routine | 執行緒執行函式起始地址,一旦執行緒被建立就會執行。 |
arg | 執行函式的引數。它必須通過把引用作為指標強制轉換為 void 型別進行傳遞。如果沒有傳遞引數,則使用 NULL。 |
建立執行緒成功時,函式返回 0,若返回值不為 0 則說明建立執行緒失敗。
終止執行緒
使用下面的程式,我們可以用它來終止一個 POSIX 執行緒:
#include <pthread.h>
pthread_exit (status)
在這裡,pthread_exit
用於顯式地退出一個執行緒。通常情況下,pthread_exit() 函式是線上程完成工作後無需繼續存在時被呼叫。
如果 main() 是在它所建立的執行緒之前結束,並通過 pthread_exit() 退出,那麼其他執行緒將繼續執行。否則,它們將在 main() 結束時自動被終止。
例項:
以下簡單的例項程式碼使用 pthread_create() 函式建立了 5 個執行緒,每個執行緒輸出"Hello Runoob!":
#include <iostream>
// 必須的標頭檔案是#include <pthread.h>using namespace std;#define NUM_THREADS 5// 執行緒的執行函式,函式返回的是函式指標,便於後面作為引數 void* say_hello(void* args){ cout << "Hello Runoob!" << endl;}int main(){ // 定義執行緒的 id 變數,多個變數使用陣列 pthread_t tids[NUM_THREADS]; for(int i = 0; i < NUM_THREADS; ++i) { //引數依次是:建立的執行緒id,執行緒引數,呼叫的函式,傳入的函式引數 int ret = pthread_create(&tids[i], NULL, say_hello, NULL); if (ret != 0) { cout << "pthread_create error: error_code=" << ret << endl; } } //等各個執行緒退出後,程序才結束,否則程序強制結束了,執行緒可能還沒反應過來; pthread_exit(NULL);}
使用 -lpthread 庫編譯下面的程式:
$ g++ test.cpp -lpthread -o test.o
現在,執行程式,將產生下列結果:
$ ./test.o
Hello Runoob!Hello Runoob!Hello Runoob!Hello Runoob!Hello Runoob!
以下簡單的例項程式碼使用 pthread_create() 函式建立了 5 個執行緒,並接收傳入的引數。每個執行緒列印一個 "Hello Runoob!" 訊息,並輸出接收的引數,然後呼叫 pthread_exit() 終止執行緒。
//檔名:test.cpp
#include <iostream>#include <cstdlib>#include <pthread.h>using namespace std;#define NUM_THREADS 5void *PrintHello(void *threadid){ // 對傳入的引數進行強制型別轉換,由無型別指標變為整形數指標,然後再讀取 int tid = *((int*)threadid); cout << "Hello Runoob! 執行緒 ID, " << tid << endl; pthread_exit(NULL);}int main (){ pthread_t threads[NUM_THREADS]; int indexes[NUM_THREADS];// 用陣列來儲存i的值 int rc; int i; for( i=0; i < NUM_THREADS; i++ ){ cout << "main() : 建立執行緒, " << i << endl; indexes[i] = i; //先儲存i的值 // 傳入的時候必須強制轉換為void* 型別,即無型別指標 rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&(indexes[i])); if (rc){ cout << "Error:無法建立執行緒," << rc << endl; exit(-1); } } pthread_exit(NULL);}
現在編譯並執行程式,將產生下列結果:
$ g++ test.cpp -lpthread -o test.o
$ ./test.omain() : 建立執行緒, 0main() : 建立執行緒, 1main() : 建立執行緒, 2main() : 建立執行緒, 3main() : 建立執行緒, 4Hello Runoob! 執行緒 ID, 4Hello Runoob! 執行緒 ID, 3Hello Runoob! 執行緒 ID, 2Hello Runoob! 執行緒 ID, 1Hello Runoob! 執行緒 ID, 0
向執行緒傳遞引數
這個例項演示瞭如何通過結構傳遞多個引數。您可以線上程回撥中傳遞任意的資料型別,因為它指向 void,如下面的例項所示:
#include <iostream>
#include <cstdlib>#include <pthread.h>using namespace std;#define NUM_THREADS 5struct thread_data{ int thread_id; char *message;};void *PrintHello(void *threadarg){ struct thread_data *my_data; my_data = (struct thread_data *) threadarg; cout << "Thread ID : " << my_data->thread_id ; cout << " Message : " << my_data->message << endl; pthread_exit(NULL);}int main (){ pthread_t threads[NUM_THREADS]; struct thread_data td[NUM_THREADS]; int rc; int i; for( i=0; i < NUM_THREADS; i++ ){ cout <<"main() : creating thread, " << i << endl; td[i].thread_id = i; td[i].message = "This is message"; rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&td[i]); //傳入到引數必須強轉為void*型別,即無型別指標 if (rc){ cout << "Error:unable to create thread," << rc << endl; exit(-1); } } pthread_exit(NULL);}
當上面的程式碼被編譯和執行時,它會產生下列結果:
$ g++ -Wno-write-strings test.cpp -lpthread -o test.o
$ ./test.omain() : creating thread, 0main() : creating thread, 1main() : creating thread, 2main() : creating thread, 3main() : creating thread, 4Thread ID : 3 Message : This is messageThread ID : 2 Message : This is messageThread ID : 0 Message : This is messageThread ID : 1 Message : This is messageThread ID : 4 Message : This is message
連線和分離執行緒
我們可以使用以下兩個函式來連線或分離執行緒:
pthread_join (threadid, status)
pthread_detach (threadid)
pthread_join()
子程式阻礙呼叫程式,直到指定的threadid
執行緒終止為止。當建立一個執行緒時,它的某個屬性會定義它是否是可連線的(joinable)或可分離的(detached)。只有建立時定義為可連線的執行緒才可以被連線。如果執行緒建立時被定義為可分離的,則它永遠也不能被連線。
這個例項演示瞭如何使用 pthread_join() 函式來等待執行緒的完成。
#include <iostream>
#include <cstdlib>#include <pthread.h>#include <unistd.h>using namespace std;#define NUM_THREADS 5void *wait(void *t){ int i; long tid; tid = (long)t; sleep(1); cout << "Sleeping in thread " << endl; cout << "Thread with id : " << tid << " ...exiting " << endl; pthread_exit(NULL);}int main (){ int rc; int i; pthread_t threads[NUM_THREADS]; pthread_attr_t attr; void *status; // 初始化並設定執行緒為可連線的(joinable) pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for( i=0; i < NUM_THREADS; i++ ){ cout << "main() : creating thread, " << i << endl; rc = pthread_create(&threads[i], NULL, wait, (void *)i ); if (rc){ cout << "Error:unable to create thread," << rc << endl; exit(-1); } } // 刪除屬性,並等待其他執行緒 pthread_attr_destroy(&attr); for( i=0; i < NUM_THREADS; i++ ){ rc = pthread_join(threads[i], &status); if (rc){ cout << "Error:unable to join," << rc << endl; exit(-1); } cout << "Main: completed thread id :" << i ; cout << " exiting with status :" << status << endl; } cout << "Main: program exiting." << endl; pthread_exit(NULL);}
當上面的程式碼被編譯和執行時,它會產生下列結果:
main() : creating thread, 0
main() : creating thread, 1main() : creating thread, 2main() : creating thread, 3main() : creating thread, 4Sleeping in thread Thread with id : 4 ...exiting Sleeping in thread Thread with id : 3 ...exiting Sleeping in thread Thread with id : 2 ...exiting Sleeping in thread Thread with id : 1 ...exiting Sleeping in thread Thread with id : 0 ...exiting Main: completed thread id :0 exiting with status :0Main: completed thread id :1 exiting with status :0Main: completed thread id :2 exiting with status :0Main: completed thread id :3 exiting with status :0Main: completed thread id :4 exiting with status :0Main: program exiting.
互斥鎖的實現
互斥鎖是實現執行緒同步的一種機制,只要在臨界區前後對資源加鎖就能阻塞其他程序的訪問。
#include <iostream>
#include <pthread.h>using namespace std;#define NUM_THREADS 5int sum = 0; //定義全域性變數,讓所有執行緒同時寫,這樣就需要鎖機制pthread_mutex_t sum_mutex; //互斥鎖void* say_hello( void* args ){ cout << "hello in thread " << *(( int * )args) << endl; pthread_mutex_lock( &sum_mutex ); //先加鎖,再修改sum的值,鎖被佔用就阻塞,直到拿到鎖再修改sum; cout << "before sum is " << sum << " in thread " << *( ( int* )args ) << endl; sum += *( ( int* )args ); cout << "after sum is " << sum << " in thread " << *( ( int* )args ) << endl; pthread_mutex_unlock( &sum_mutex ); //釋放鎖,供其他執行緒使用 pthread_exit( 0 );}int main(){ pthread_t tids[NUM_THREADS]; int indexes[NUM_THREADS]; pthread_attr_t attr; //執行緒屬性結構體,建立執行緒時加入的引數 pthread_attr_init( &attr ); //初始化 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); //是設定你想要指定執行緒屬性引數,這個引數表明這個執行緒是可以join連線的,join功能表示主程式可以等執行緒結束後再去做某事,實現了主程式和執行緒同步功能 pthread_mutex_init( &sum_mutex, NULL ); //對鎖進行初始化 for( int i = 0; i < NUM_THREADS; ++i ) { indexes[i] = i; int ret = pthread_create( &tids[i], &attr, say_hello, ( void* )&( indexes[i] ) ); //5個程序同時去修改sum if( ret != 0 ) { cout << "pthread_create error:error_code=" << ret << endl; } } pthread_attr_destroy( &attr ); //釋放記憶體 void *status; for( int i = 0; i < NUM_THREADS; ++i ) { int ret = pthread_join( tids[i], &status ); //主程式join每個執行緒後取得每個執行緒的退出資訊status if( ret != 0 ) { cout << "pthread_join error:error_code=" << ret << endl; } } cout << "finally sum is " << sum << endl; pthread_mutex_destroy( &sum_mutex ); //登出鎖}
測試結果:
hello in thread hello in thread 1hello in thread 3
0hello in thread 2before sum is 0 in thread 1hello in thread 4after sum is 1 in thread 1before sum is 1 in thread 3after sum is 4 in thread 3before sum is 4 in thread 4after sum is 8 in thread 4before sum is 8 in thread 0after sum is 8 in thread 0before sum is 8 in thread 2after sum is 10 in thread 2finally sum is 10
可知,sum的訪問和修改順序是正常的,這就達到了多執行緒的目的了,但是執行緒的執行順序是混亂的,混亂就是正常?
訊號量的實現
訊號量是執行緒同步的另一種實現機制,訊號量的操作有signal
和wait
,本例子採用條件訊號變數
pthread_cond_t tasks_cond;
訊號量的實現也要給予鎖機制。
#include <iostream>
#include <pthread.h>#include <stdio.h>using namespace std;#define BOUNDARY 5int tasks = 10;pthread_mutex_t tasks_mutex; //互斥鎖pthread_cond_t tasks_cond; //條件訊號變數,處理兩個執行緒間的條件關係,當task>5,hello2處理,反之hello1處理,直到task減為0void* say_hello2( void* args ){ pthread_t pid = pthread_self(); //獲取當前執行緒id cout << "[" << pid << "] hello in thread " << *( ( int* )args ) << endl; bool is_signaled = false; //sign while(1) { pthread_mutex_lock( &tasks_mutex ); //加鎖 if( tasks > BOUNDARY ) { cout << "[" << pid << "] take task: " << tasks << " in thread " << *( (int*)args ) << endl; --tasks; //modify } else if( !is_signaled ) { cout << "[" << pid << "] pthread_cond_signal in thread " << *( ( int* )args ) << endl; pthread_cond_signal( &tasks_cond ); //signal:向hello1傳送訊號,表明已經>5 is_signaled = true; //表明訊號已傳送,退出此執行緒 } pthread_mutex_unlock( &tasks_mutex ); //解鎖 if( tasks == 0 ) break; }}void* say_hello1( void* args ){ pthread_t pid = pthread_self(); //獲取當前執行緒id cout << "[" << pid << "] hello in thread " << *( ( int* )args ) << endl; while(1) { pthread_mutex_lock( &tasks_mutex ); //加鎖 if( tasks > BOUNDARY ) { cout << "[" << pid << "] pthread_cond_signal in thread " << *( ( int* )args ) << endl; pthread_cond_wait( &tasks_cond, &tasks_mutex ); //wait:等待訊號量生效,接收到訊號,向hello2發出訊號,跳出wait,執行後續 } else { cout << "[" << pid << "] take task: " << tasks << " in thread " << *( (int*)args ) << endl; --tasks; } pthread_mutex_unlock( &tasks_mutex ); //解鎖 if( tasks == 0 ) break; }}int main(){ pthread_attr_t attr; //執行緒屬性結構體,建立執行緒時加入的引數 pthread_attr_init( &attr ); //初始化 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); //是設定你想要指定執行緒屬性引數,這個引數表明這個執行緒是可以join連線的,join功能表示主程式可以等執行緒結束後再去做某事,實現了主程式和執行緒同步功能 pthread_cond_init( &tasks_cond, NULL ); //初始化條件訊號量 pthread_mutex_init( &tasks_mutex, NULL ); //初始化互斥量 pthread_t tid1, tid2; //儲存兩個執行緒id int index1 = 1; int ret = pthread_create( &tid1, &attr, say_hello1, ( void* )&index1 ); if( ret != 0 ) { cout << "pthread_create error:error_code=" << ret << endl; } int index2 = 2; ret = pthread_create( &tid2, &attr, say_hello2, ( void* )&index2 ); if( ret != 0 ) { cout << "pthread_create error:error_code=" << ret << endl; } pthread_join( tid1, NULL ); //連線兩個執行緒 pthread_join( tid2, NULL ); pthread_attr_destroy( &attr ); //釋放記憶體 pthread_mutex_destroy( &tasks_mutex ); //登出鎖 pthread_cond_destroy( &tasks_cond ); //正常退出}
測試結果:
先線上程2中執行say_hello2,再跳轉到執行緒1中執行say_hello1,直到tasks減到0為止。
[2] hello in thread 1
[2] pthread_cond_signal in thread 1[3] hello in thread 2[3] take task: 10 in thread 2[3] take task: 9 in thread 2[3] take task: 8 in thread 2[3] take task: 7 in thread 2[3] take task: 6 in thread 2[3] pthread_cond_signal in thread 2[2] take task: 5 in thread 1[2] take task: 4 in thread 1[2] take task: 3 in thread 1[2] take task: 2 in thread 1[2] take task: 1 in thread 1
C++ 11中的多執行緒技術
C++11 新標準中引入了四個標頭檔案來支援多執行緒程式設計,他們分別是
<atomic>
,<thread>
,<mutex>
,<condition_variable>
和<future>
。
-
<atomic>
:提供原子操作功能,該頭文主要聲明瞭兩個類, std::atomic 和 std::atomic_flag,另外還聲明瞭一套 C 風格的原子型別和與 C 相容的原子操作的函式。 -
<thread>
:執行緒模型封裝,該標頭檔案主要聲明瞭 std::thread 類,另外 std::this_thread 名稱空間也在該標頭檔案中。 -
<mutex>
:互斥量封裝,該標頭檔案主要聲明瞭與互斥量(mutex)相關的類,包括 std::mutex 系列類,std::lock_guard, std::unique_lock, 以及其他的型別和函式。 -
<condition_variable>
:條件變數,該標頭檔案主要聲明瞭與條件變數相關的類,包括 std::condition_variable 和 std::condition_variable_any。 -
<future>
:實現了對指定資料提供者提供的資料進行非同步訪問的機制。該標頭檔案主要聲明瞭 std::promise, std::package_task 兩個 Provider 類,以及 std::future 和 std::shared_future 兩個 Future 類,另外還有一些與之相關的型別和函式,std::async() 函式就宣告在此標頭檔案中。
簡單示例:
#include <iostream>
#include <thread>using namespace std;void thread_1(){ cout << "hello from thread_1" << endl;}int main(int argc, char **argv){ thread t1(thread_1); /** join()相當於呼叫了兩個函式:WaitForSingleObject、CloseHandle,事實上,在vc12中也是這麼實現的 */ t1.join(); return 0;}
注意事項
-
若執行緒呼叫到的函式在一個類中,則必須將該函式宣告為靜態函式函式
因為靜態成員函式屬於靜態全域性區,執行緒可以共享這個區域,故可以各自呼叫。
#include <iostream>
#include <pthread.h> using namespace std; #define NUM_THREADS 5 class Hello { public: //多執行緒呼叫,宣告為static static void* say_hello( void* args ) { cout << "hello..." << endl; } }; int main() { pthread_t tids[NUM_THREADS]; for( int i = 0; i < NUM_THREADS; ++i ) { int ret = pthread_create( &tids[i], NULL, Hello::say_hello, NULL ); if( ret != 0 ) { cout << "pthread_create error:error_code" << ret << endl; } } pthread_exit( NULL ); }
測試結果:
hello...
hello... hello... hello... hello...
-
程式碼中如果沒有
pthread_join
主執行緒會很快結束從而使整個程序結束,從而使建立的執行緒沒有機會開始執行就結束了。加入pthread_join
後,主執行緒會一直等待直到等待的執行緒結束自己才結束,使建立的執行緒有機會執行。 -
執行緒建立時屬性引數的設定pthread_attr_t及join功能的使用
執行緒的屬性由結構體pthread_attr_t進行管理。
typedef struct
{ int detachstate; //執行緒的分離狀態 int schedpolicy; //執行緒排程策略 struct sched_param schedparam; //執行緒的排程引數 int inheritsched; //執行緒的繼承性 int scope; //執行緒的作用域 size_t guardsize; //執行緒棧末尾的警戒緩衝區大小 int stackaddr_set; void * stackaddr; //執行緒棧的位置 size_t stacksize; // 執行緒棧的大小}pthread_attr_t;
示例:
#include <iostream>
#include <pthread.h> using namespace std; #define NUM_THREADS 5 void* say_hello( void* args ) { cout << "hello in thread " << *(( int * )args) << endl; int status = 10 + *(( int * )args); //執行緒退出時新增退出的資訊,status供主程式提取該執行緒的結束資訊 pthread_exit( ( void* )status ); } int main() { pthread_t tids[NUM_THREADS]; int indexes[NUM_THREADS]; pthread_attr_t attr; //執行緒屬性結構體,建立執行緒時加入的引數 pthread_attr_init( &attr ); //初始化 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); //是設定你想要指定執行緒屬性引數,這個引數表明這個執行緒是可以join連線的,join功能表示主程式可以等執行緒結束後再去做某事,實現了主程式和執行緒同步功能 for( int i = 0; i < NUM_THREADS; ++i ) { indexes[i] = i; int ret = pthread_create( &tids[i], &attr, say_hello, ( void* )&( indexes[i] ) ); if( ret != 0 ) { cout << "pthread_create error:error_code=" << ret << endl; } } pthread_attr_destroy( &attr ); //釋放記憶體 void *status; for( int i = 0; i < NUM_THREADS; ++i ) { int ret = pthread_join( tids[i], &status ); //主程式join每個執行緒後取得每個執行緒的退出資訊status if( ret != 0 ) { cout << "pthread_join error:error_code=" << ret << endl; } else { cout << "pthread_join get status:" << (long)status << endl; } } }
測試結果:
hello in thread hello in thread 1hello in thread 3
hello in thread 40hello in thread 2pthread_join get status:10pthread_join get status:11pthread_join get status:12pthread_join get status:13pthread_join get status:14
相關推薦
linux下C開發多執行緒程式
轉:https://blog.csdn.net/lingfemg721/article/details/6574804 linux下用C開發多執行緒程式,Linux系統下的多執行緒遵循POSIX執行緒介面,稱為pthread。 #
Linux下c語言多執行緒程式設計
執行緒的資料處理 和程序相比,執行緒的最大優點之一是資料的共享性,各個程序共享父程序處沿襲的資料段,可以方便的獲得、修改資料。但這也給多執行緒程式設計帶來了許多問題。我們必須當心有多個不同的程序訪問相同的變數。許多函式是不可重入的,即同時不能執行一個函式的多個拷貝(除非使用不同的資料段)。在函式中宣告的靜
linux下C語言多執行緒(二)執行緒的私有資料
二. 建立和登出 Posix定義了兩個API分別用來建立和登出TSD: int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *)); 該函式從TSD池中分配一項,將其值賦給key供以後訪問使用。如果destr_fu
linux下C語言多執行緒(一)執行緒的建立與取消
#include <pthread.h> int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void
Linux平臺上用C++實現多執行緒互斥鎖
在上篇用C++實現了Win32平臺上的多執行緒互斥鎖,這次寫個Linux平臺上的,同樣參考了開源專案C++ Sockets的程式碼,在此對這些給開源專案做出貢獻的鬥士們表示感謝! 下邊分別是互斥鎖類和測試程式碼,已經在Fedora 13虛擬機器上測試通過。
用C++實現多執行緒Mutex鎖(Win32)
本文目的:用C++和Windows的互斥物件(Mutex)來實現執行緒同步鎖。 準備知識:1,核心物件互斥體(Mutex)的工作機理,WaitForSingleObject函式的用法,這些可以從MSDN獲取詳情; 2,當兩個或更多執行緒需要同時訪問一個共享資
Linux下gdb除錯多執行緒
gdb除錯多執行緒,目前我知道的就以下幾條指令,當然編譯的時候需要加-g選項。 info threads 顯示所有執行緒,系統會給每個執行緒都分配一個編號,編號前帶“*”的,是當前正在除錯的執行緒
Linux-(C)多執行緒學習(入門)
下面兩個仁兄總結非常好。 主要學習一個例子: /* * test1.c * * Created on: 2016年7月26日 * Author: Andy_Cong
windows程式設計 使用C++實現多執行緒類
本文簡單介紹如何在windows程式設計中實現多執行緒類,供大家學習參考,也希望大家指正。 有時候我們想在一個類中實現多執行緒,主執行緒在某些時刻獲得資料,可以“通知”子執行緒去處理,然後把結果返回。下面的例項是主執行緒每隔2s產生10個隨機數,將這10隨機數傳給多執行緒
C++實現多執行緒物件記憶體池帶垃圾回收機制
#include <Windows.h> #include <iostream> #include <map> #include <string> #include <assert.h> #include <
linux下gdb除錯多執行緒死迴圈
1、我們首先要知道是哪個執行緒出了問題: A、查程序 ps -ef | grep 程序名 B、查執行緒 top -H -p 程序ID 2、gdb檢視 gdb 程序名 程序號 : A、檢視是哪個執行緒出了問題 (gdb) inf
QT C++實現多執行緒通訊--示例程式碼
</pre><p></p><pre name="code" class="cpp">先看測試程式碼:main.cpp#include "mainwindow.h" #include <QApplication> /
linux下 C++如何實現多執行緒
多執行緒是多工處理的一種特殊形式,多工處理允許讓電腦同時執行兩個或兩個以上的程式。一般情況下,兩種型別的多工處理:基於程序和基於執行緒。 基於程序的多工處理是程式的併發執行。 執行緒的多工處理是同一程式的片段的併發執行。 多執行緒程式包含可以同時執行的兩個或多個
基於TCP協議實現Linux下客戶端與伺服器之間的通訊,實現多執行緒、多程序伺服器
TCP是TCP/IP協議族中一個比較重要的協議,這是一種可靠、建立連結、面向位元組流的傳輸,工作在傳輸層。和TCP相對的不可靠、無連結、面向資料報的協議UDP,瞭解UDP客戶端與伺服器之間通訊請戳UDP協議實現的伺服器與客戶端通訊 TCP協議建立連線 首
Linux多執行緒學習(5)--C++實現一個執行緒池
多執行緒學習總結(1):https://blog.csdn.net/hansionz/article/details/84665815 多執行緒學習總結(2):https://blog.csdn.net/hansionz/article/details/84675536 多執行緒學習總結
Java併發(十八):阻塞佇列BlockingQueue BlockingQueue(阻塞佇列)詳解 二叉堆(一)之 圖文解析 和 C語言的實現 多執行緒程式設計:阻塞、併發佇列的使用總結 Java併發程式設計:阻塞佇列 java阻塞佇列 BlockingQueue(阻塞佇列)詳解
阻塞佇列(BlockingQueue)是一個支援兩個附加操作的佇列。 這兩個附加的操作是:在佇列為空時,獲取元素的執行緒會等待佇列變為非空。當佇列滿時,儲存元素的執行緒會等待佇列可用。 阻塞佇列常用於生產者和消費者的場景,生產者是往佇列裡新增元素的執行緒,消費者是從佇列裡拿元素的執行緒。阻塞佇列就是生產者
C# 利用執行緒安全資料結構BlockingCollection實現多執行緒
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; using Danny.Infrastructure.Helper; names
Qt下實現多執行緒的串列埠通訊
簡述 Qt下無論是RS232、RS422、RS485的串列埠通訊都可以使用統一的編碼實現。本文把每路串列埠的通訊各放在一個執行緒中,使用movetoThread的方式實現。 程式碼之路 用SerialPort類實現串列埠功能,Widget類呼叫串列埠。 serialport.h如
C++ 11 多執行緒下std::unique_lock與std::lock_guard的區別和用法
這裡主要介紹std::unique_lock與std::lock_guard的區別用法 先說簡單的 一、std::lock_guard的用法 std::lock_guard其實就是簡單的RAII封裝,在建構函式中進行加鎖,解構函式中進行解鎖,這樣可以保證函式退出時,鎖一定被釋放。 簡單來說,就是防止開
C++11 併發指南九(綜合運用: C++11 多執行緒下生產者消費者模型詳解)
前面八章介紹了 C++11 併發程式設計的基礎(抱歉哈,第五章-第八章還在草稿中),本文將綜合運用 C++11 中的新的基礎設施(主要是多執行緒、鎖、條件變數)來闡述一個經典問題——生產者消費者模型,並給出完整的解決方案。 生產者消費者問題是多執行緒併發中一個非常經典的問題,相信學過作業系統課程的同學都清楚