本蒻菊來向大佬們請教啦,我用的是Dev C++ 5.1!其實就是實作一個簡單的效果(模擬了Python中的執行緒類):
#include <iostream>
#include <algorithm>
//執行緒類和執行緒池類,放在C盤了
#include <C:/Thread.h>
#include <C:/ThreadPool.h>
// 或者合在一起:#include<C:/Threads.h>
using namespace std;
int inplus()
{
int a, b;
cout << "輸入a, b:";
cin >> a >> b;
int c = a + b;
cout << "答案:" << c << endl;
return c;
}
void sortlist(int len)
{
int *list = new int[len];
for (int i = 0; i < len; i++) cin >> list[i];
sort(list, list + len + 1);
cout << "排序結果:";
for (int i = 0; i < len; i++) cout << list[i] << ' ';
cout << endl;
}
int main()
{
Thread t1(inplus), t2(sortlist, 5); //定義(未就緒)
t1.start(); //啟動(開始就緒,等待運行)
t2.start();
t1.wait(); //等待運行結束
t2.wait();
//...其他效果后面說
return 0;
}
后面效果我來評論下來。
uj5u.com熱心網友回復:
先說Thread類的效果:1.定義(未就緒)
Thread name(function, args...);
就是定義一個名字叫name的Thread物件,function是任意回傳值有任意引數的一個函式,args是可選/多個引數,用來寫function函式的引數。(例如Thread t1(sortlist, 一個數);)
2.啟動(就緒)
name.start();
讓name物件啟動
3.等待結束
name.wait();
程式可以繼續運行但是當name物件所有任務完成時會提醒主程式,具體咋提醒看用戶
4.其他
執行緒基本操作:sleep(),...
5.立即結束
比如有一個是while(1)一直回圈,可以用
name.end()
直接結束,比如按鍵監控。
uj5u.com熱心網友回復:
然后是ThreadPool類的:ThreadPool tp1; //創建執行緒池
ThreadPool tp2(5); //創建執行緒池并創建5個執行緒
tp1.CreateThread(n); //新添n個執行緒在執行緒(默認引數=1)
還有:
uj5u.com熱心網友回復:
我是Windows10!!!轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/254132.html
標籤:C++ 語言
上一篇:C語言中MD5加密的問題
