文章目錄
- 九、多線程
- 2. 線程的控制
- 未完待續
九、多線程
2. 線程的控制
主線程退出 等同于 進程退出 等同于 所有線程都退出。為了避免主線程退出,但是新線程并沒有執行完自己的任務的問題,主線程同樣要跟進程一樣等待新線程返回。
pthread_join 函數則可以等待新線程返回。
Makefile:
test_thread: testThread.ccg++ -o $@ $^ -std=c++11 -lpthread
.PHONY: clean
clean:rm -f test_thread
testThread.cc:
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;// 新線程
void* newpthreadrun(void* arg)
{int cnt = 5;while (cnt--){cout << "I am newpthreadrun thread" << endl;sleep(1);}
}int main()
{pthread_t tid;pthread_create(&tid, nullptr, newpthreadrun, nullptr);// 主線程pthread_join(tid, nullptr);cout << "I am main thread" << endl;sleep(5);return 0;
}
多線程中,任何一個線程出現異常,都會導致整個進程退出 ---- 多線程代碼往往健壯性不好。 不能用 exit() 來退出線程,因為 exit() 是退出進程的,可以使用 pthread_exit() 來退出線程,也可以在主線程中使用 pthread_cancel() 來取消新線程線程,新線程會返回 -1。
我們來創建一個多線程:
Makefile:
test_thread: testThread.ccg++ -o $@ $^ -std=c++11 -lpthread
.PHONY: clean
clean:rm -f test_thread
testThread.cc:
#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;const int threadnum = 5;// 加法任務
class Task
{
public:Task(int x, int y):datax(x),datay(y){}int Excute(){return datax + datay;}~Task() {}
private:int datax;int datay;
};// 線程數據
class ThreadData
{
public:ThreadData(int x, int y, const string& threadname):t(x, y),threadname(threadname){}string Threadname(){return threadname;}int run(){return t.Excute();}~ThreadData() {}
private:string threadname;Task t;
};// 執行任務后的結果
class Result
{
public:Result() {}void SetResult(int result, const string& threadname){_result = result;_threadname = threadname;}void Print(){cout << _threadname << " run result: " << _result << endl;}~Result() {}
private:int _result;string _threadname;
};// 線程執行任務
void* handlerTask(void* args)
{ThreadData* td = static_cast<ThreadData*>(args);string name = td->Threadname();Result* res = new Result();int result = td->run();res->SetResult(result, name);return res;
}int main()
{// 創建線程并分配任務vector<pthread_t> threads;for (int i = 0; i < threadnum; i++){char threadname[64];snprintf(threadname, 64, "Thread-%d", i + 1);ThreadData* td = new ThreadData(10, 20, threadname);pthread_t tid;pthread_create(&tid, nullptr, handlerTask, td);threads.push_back(tid);}// 等待線程執行完畢并獲取結果vector<Result*> result_set;void* ret = nullptr;for (auto& tid : threads){pthread_join(tid, &ret);result_set.push_back((Result*)(ret));}// 打印結果for (auto& res : result_set){res->Print();delete res;}return 0;
}
執行結果: