C/C++ 多線程的學習心得總結(jié)
更新時間:2013年05月30日 17:54:01 作者:
本篇文章是對C/C++中多線程的學習心得總結(jié)進行了詳細的分析介紹,需要的朋友參考下
個人覺得在學習多線程編程之前最好先了解進程和線程的關(guān)系, 然后在學習線程工作方式的過程中動手寫個(我是從抄開始的)多線程的小程序, 會對學習多線程有很大的幫助, 否則只有理論是很抽象的.
在學習多線程編程之前, 必須先知道什么是 線程函數(shù), 線程函數(shù)就是另一個線程的入口函數(shù). 默認情況下一個我們所寫的代碼都是只有一個線程的, 而這個線程的入口函數(shù)就是main() 函數(shù), 這是系統(tǒng)默認的. 而我們創(chuàng)建的另一個線程也需要一個函數(shù)來進入, 這個函數(shù)就叫做線程函數(shù).
在C/C++中, 可以調(diào)用 '運行期庫' 函數(shù) _beginthreadex(...), 來創(chuàng)建一個線程, _beginthreadex(...)函數(shù)接受6個參數(shù), 其中第3個參數(shù)要求傳入該線程的入口函數(shù)的地址(即:傳入線程函數(shù)的函數(shù)名+&即可), 其它的每個參數(shù)的意思可以在msdn或網(wǎng)上查一下, 入門的話其余參數(shù)全部傳0即可.
_beginthreadex(...)函數(shù)返回一個句柄, 就是新線程的句柄. 對于傳入的線程函數(shù)是有要求的, 線程函數(shù)必須返回 unsigned __stdcall 類型, 并且接受一個 void* 型參數(shù), 函數(shù)的內(nèi)容就可以自己定了.
先來段多線程的簡單代碼:
#include<windows.h>
#include<process.h>
#include<iostream>
using namespace std;
bool stop;
unsigned Counter;
unsigned __stdcall thread(void*)
{
cout <<"In second thread..." <<endl;
while (!stop){
Sleep(200);
cout <<Counter++ <<" " <<flush;
}
//_endthreadex(0);
return 0;
}
int main()
{
HANDLE hThread;
unsigned int threadID;
stop = false;
cout <<"Creating second thread..." <<endl;
// Create the second thread.
hThread = (HANDLE)_beginthreadex(NULL, 0, &thread, NULL, 0, &threadID);
// Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
//WaitForSingleObject(hThread, INFINITE);
system("pause");
stop = true;
//cin >>stop;
cout <<"Counter is-> " <<Counter <<endl;
// Destroy the thread object.
CloseHandle(hThread);
system("pause");
return 0;
}
代碼是正確的, 復(fù)制粘貼到 Visual C++ 6.0 中, 即可編譯運行. 但是會發(fā)現(xiàn) _beginthreadex(...) 函數(shù)未定義的編譯錯誤. 原因是因為我們的visual c++ 6.0 默認的是在單線程模式下編程, 如果需要進行多線程編程, 需要轉(zhuǎn)換一下編譯器的 '運行期庫' , 方法很簡單:
Project --> Settings --> C/C++ 中 選擇 Category 中的 Code generation , 然后在 Use run-time library 里面選含有Multithread的其中一個就可以了.
這時再編譯, 就可以通過了. 有了這個例子的函數(shù), 現(xiàn)在應(yīng)該已經(jīng)入門了吧. 不過除了這個還有很多基礎(chǔ)知識要補上, 推薦通讀幾遍 <windows核心編程(第四版)> 的第6章.
實際上, 在不同的編譯環(huán)境下, _beginthreadex(...)函數(shù)可能會有不同的名稱, 但是它們都是對 windows函數(shù) CreateThread(...) 的封裝, CreateThread(...)函數(shù)用來創(chuàng)建一個新的線程函數(shù), CreateThread(...)函數(shù)同樣接受6個參數(shù), 在類似 _beginthreadex(...)的函數(shù)中對其做了一些安全的處理, 如堆棧內(nèi)存的申請等. 雖然可以用CreateThread函數(shù)來創(chuàng)建一個新的線程, 但是強烈建議使用 _beginthreadex(...)函數(shù), 對于CreateThread 函數(shù)在<windows核心編程(第四版)>第6章中有很好的講解.
當一個線程的任務(wù)結(jié)束, 要退出時, 有四種方法:
1.線程函數(shù)返回(最好使用這種方法);
2.通過調(diào)用 _endthreadex()或 ExitThread()函數(shù),線程將自行撤消(最好不要使用這種方法);
3.同一個進程或另一個進程中的線程調(diào)用 TerminateThread()函數(shù)(應(yīng)該避免使用這種方法);
4.包含線程的進程終止運行(應(yīng)該避免使用這種方法).
最好通過其入口函數(shù)的返回語句(即:return)來退出線程, 也可以通過調(diào)用 C/C++ '運行期庫'函數(shù) _endthreadex()函數(shù)來退出, 還有兩種退出的方法, 都是類似的強制退出. 最好使用線程函數(shù)的返回語句(return)來退出線程, 只有這樣才能安全的回收該線程的處理器資源和內(nèi)存資源. 而實際上 _endthreadex()函數(shù)和 _beginthreadex(...)函數(shù)一樣, 是封裝了windows函數(shù) ExitThread(), 如果一定要強制退出線程, 那么強烈建議調(diào)用 _endthreadex() 函數(shù), 這樣能安全的回收系統(tǒng)資源.
具體的 _beginthreadex(...)函數(shù) 和 _endthreadex()函數(shù)在<windows核心編程(第四版)>第6章中有很好的剖析. 同時還有兩個 _beginthread(...) 和 _endthread() 運行期庫函數(shù), 也有講解.
windows還提供了一些庫函數(shù)用來獲得當前進程或者線程的句柄, 如 HANDLE GetCurrentProcess() 函數(shù)返回當前進程的句柄, HANDLE GetCurrentThread() 函數(shù)返回當前線程的句柄, 但是需要注意的是, 這些句柄都是 '偽句柄' , 即: 只在本進程和本線程內(nèi)可用, 不可傳出取用.
如果需要在外部使用其它線程或進程的句柄, 則可以用 DuplicateHandle(...) 函數(shù)獲得其它進程或者線程的 '實句柄' , 該函數(shù)接受7個參數(shù), 具體的用法可以查閱一下msdn和網(wǎng)絡(luò). 需要注意的是, 在使用完 由DuplicateHandle(...) 函數(shù)獲得的句柄后, 需要使用 CloseHandle() 函數(shù)來關(guān)閉該句柄.
在學習多線程編程之前, 必須先知道什么是 線程函數(shù), 線程函數(shù)就是另一個線程的入口函數(shù). 默認情況下一個我們所寫的代碼都是只有一個線程的, 而這個線程的入口函數(shù)就是main() 函數(shù), 這是系統(tǒng)默認的. 而我們創(chuàng)建的另一個線程也需要一個函數(shù)來進入, 這個函數(shù)就叫做線程函數(shù).
在C/C++中, 可以調(diào)用 '運行期庫' 函數(shù) _beginthreadex(...), 來創(chuàng)建一個線程, _beginthreadex(...)函數(shù)接受6個參數(shù), 其中第3個參數(shù)要求傳入該線程的入口函數(shù)的地址(即:傳入線程函數(shù)的函數(shù)名+&即可), 其它的每個參數(shù)的意思可以在msdn或網(wǎng)上查一下, 入門的話其余參數(shù)全部傳0即可.
_beginthreadex(...)函數(shù)返回一個句柄, 就是新線程的句柄. 對于傳入的線程函數(shù)是有要求的, 線程函數(shù)必須返回 unsigned __stdcall 類型, 并且接受一個 void* 型參數(shù), 函數(shù)的內(nèi)容就可以自己定了.
先來段多線程的簡單代碼:
復(fù)制代碼 代碼如下:
#include<windows.h>
#include<process.h>
#include<iostream>
using namespace std;
bool stop;
unsigned Counter;
unsigned __stdcall thread(void*)
{
cout <<"In second thread..." <<endl;
while (!stop){
Sleep(200);
cout <<Counter++ <<" " <<flush;
}
//_endthreadex(0);
return 0;
}
int main()
{
HANDLE hThread;
unsigned int threadID;
stop = false;
cout <<"Creating second thread..." <<endl;
// Create the second thread.
hThread = (HANDLE)_beginthreadex(NULL, 0, &thread, NULL, 0, &threadID);
// Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
//WaitForSingleObject(hThread, INFINITE);
system("pause");
stop = true;
//cin >>stop;
cout <<"Counter is-> " <<Counter <<endl;
// Destroy the thread object.
CloseHandle(hThread);
system("pause");
return 0;
}
代碼是正確的, 復(fù)制粘貼到 Visual C++ 6.0 中, 即可編譯運行. 但是會發(fā)現(xiàn) _beginthreadex(...) 函數(shù)未定義的編譯錯誤. 原因是因為我們的visual c++ 6.0 默認的是在單線程模式下編程, 如果需要進行多線程編程, 需要轉(zhuǎn)換一下編譯器的 '運行期庫' , 方法很簡單:
Project --> Settings --> C/C++ 中 選擇 Category 中的 Code generation , 然后在 Use run-time library 里面選含有Multithread的其中一個就可以了.
這時再編譯, 就可以通過了. 有了這個例子的函數(shù), 現(xiàn)在應(yīng)該已經(jīng)入門了吧. 不過除了這個還有很多基礎(chǔ)知識要補上, 推薦通讀幾遍 <windows核心編程(第四版)> 的第6章.
實際上, 在不同的編譯環(huán)境下, _beginthreadex(...)函數(shù)可能會有不同的名稱, 但是它們都是對 windows函數(shù) CreateThread(...) 的封裝, CreateThread(...)函數(shù)用來創(chuàng)建一個新的線程函數(shù), CreateThread(...)函數(shù)同樣接受6個參數(shù), 在類似 _beginthreadex(...)的函數(shù)中對其做了一些安全的處理, 如堆棧內(nèi)存的申請等. 雖然可以用CreateThread函數(shù)來創(chuàng)建一個新的線程, 但是強烈建議使用 _beginthreadex(...)函數(shù), 對于CreateThread 函數(shù)在<windows核心編程(第四版)>第6章中有很好的講解.
當一個線程的任務(wù)結(jié)束, 要退出時, 有四種方法:
1.線程函數(shù)返回(最好使用這種方法);
2.通過調(diào)用 _endthreadex()或 ExitThread()函數(shù),線程將自行撤消(最好不要使用這種方法);
3.同一個進程或另一個進程中的線程調(diào)用 TerminateThread()函數(shù)(應(yīng)該避免使用這種方法);
4.包含線程的進程終止運行(應(yīng)該避免使用這種方法).
最好通過其入口函數(shù)的返回語句(即:return)來退出線程, 也可以通過調(diào)用 C/C++ '運行期庫'函數(shù) _endthreadex()函數(shù)來退出, 還有兩種退出的方法, 都是類似的強制退出. 最好使用線程函數(shù)的返回語句(return)來退出線程, 只有這樣才能安全的回收該線程的處理器資源和內(nèi)存資源. 而實際上 _endthreadex()函數(shù)和 _beginthreadex(...)函數(shù)一樣, 是封裝了windows函數(shù) ExitThread(), 如果一定要強制退出線程, 那么強烈建議調(diào)用 _endthreadex() 函數(shù), 這樣能安全的回收系統(tǒng)資源.
具體的 _beginthreadex(...)函數(shù) 和 _endthreadex()函數(shù)在<windows核心編程(第四版)>第6章中有很好的剖析. 同時還有兩個 _beginthread(...) 和 _endthread() 運行期庫函數(shù), 也有講解.
windows還提供了一些庫函數(shù)用來獲得當前進程或者線程的句柄, 如 HANDLE GetCurrentProcess() 函數(shù)返回當前進程的句柄, HANDLE GetCurrentThread() 函數(shù)返回當前線程的句柄, 但是需要注意的是, 這些句柄都是 '偽句柄' , 即: 只在本進程和本線程內(nèi)可用, 不可傳出取用.
如果需要在外部使用其它線程或進程的句柄, 則可以用 DuplicateHandle(...) 函數(shù)獲得其它進程或者線程的 '實句柄' , 該函數(shù)接受7個參數(shù), 具體的用法可以查閱一下msdn和網(wǎng)絡(luò). 需要注意的是, 在使用完 由DuplicateHandle(...) 函數(shù)獲得的句柄后, 需要使用 CloseHandle() 函數(shù)來關(guān)閉該句柄.
相關(guān)文章
C++非遞歸隊列實現(xiàn)二叉樹的廣度優(yōu)先遍歷
這篇文章主要介紹了C++非遞歸隊列實現(xiàn)二叉樹的廣度優(yōu)先遍歷,實例分析了遍歷二叉樹相關(guān)算法技巧,并附帶了兩個相關(guān)算法實例,需要的朋友可以參考下2015-07-07VC外部符號錯誤_main,_WinMain@16,__beginthreadex解決方法
這篇文章主要介紹了VC外部符號錯誤_main,_WinMain@16,__beginthreadex解決方法,實例分析了比較典型的錯誤及對應(yīng)的解決方法,需要的朋友可以參考下2015-05-05C語言編程數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)詳解小白篇
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ),非常適合初學數(shù)據(jù)結(jié)構(gòu)的小白,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家多多進步,早日升職加薪2021-09-09