代码拉取完成,页面将自动刷新
#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <thread>
using namespace std;
#include <Windows.h>
void threadfun(std::string text) {
Sleep(10);
cout << text << endl;
}
class sTest1 {
public:
sTest1() {}
~sTest1() {}
void test(int x) {
cout << x << ",tid:" << std::this_thread::get_id() << endl;
}
void test1(int x1, int x2, int x3) {
cout << "x1: " << x1;
cout << ", x2: " << x2;
cout << ", x3: " << x3 << endl;
}
};
void test1() {
auto pt = [](std::thread* td) {
cout << "joinable:" << (td->joinable() ? "true":"false") << endl;
};
sTest1 t1;
std::unique_ptr<std::thread> td = nullptr;
td = std::make_unique<std::thread>(
std::bind(&sTest1::test, &t1, 10));
pt(td.get());
td->join();
pt(td.get());
td = std::make_unique<std::thread>(
std::bind(&sTest1::test, &t1, 11));
pt(td.get());
td->join();
pt(td.get());
std::thread tdd;
pt(&tdd);
}
void test2() {
std::thread td;
cout << td.get_id() << endl;
sTest1 t1;
auto fun1 = std::bind(
&sTest1::test1
, &t1
, std::placeholders::_1
, std::placeholders::_2
, std::placeholders::_3 );
std::unique_ptr<std::thread> td1 = std::make_unique<std::thread>(
fun1, 10 , 20, 30
);
td1->join();
}
int main(int argc, char** argv)
{
cout << "hello world!" << endl;
std::thread td1(threadfun, "td1");
std::thread td2(threadfun, "td2");
std::thread td3(threadfun, "td3");
std::thread td4(threadfun, "td4");
std::thread td5(threadfun, "td5");
td1.detach();
td2.detach();
td3.detach();
td4.detach();
td5.join();
cout << "main td" << endl;
cout << "-------test1-------" << endl;
test1();
cout << "-------test2-------" << endl;
test2();
Sleep(100);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。