1 Star 0 Fork 0

Dzlua/test

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.cpp 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
Dzlua 提交于 2017-07-09 12:25 +08:00 . thread 测试
#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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/dzlua/test.git
git@gitee.com:dzlua/test.git
dzlua
test
test
master

搜索帮助