int mythread()
{
cout << "mythread() start" << " thread id = " << std::this_thread::get_id() << endl;
std::this_thread::sleep_for(2s);
cout << "mythread() end " << " thread id = " << std::this_thread::get_id() << endl;
return 5;
}
int main()
{
cout << "main id = " << std::this_thread::get_id() << endl;
//async 异步,等全部线程执行完主函数才返回
//std::future<int> result = std::async(mythread);
std::future<int> result = std::async(std::launch::deferred,mythread);
/*cout << result.get() << endl;*/
//枚举类型
std::future_status status = result.wait_for(std::chrono::seconds(3));
if (status == std::future_status::timeout)
{
//超时表示线程还没执行完;
cout << " time out " << endl;
}
else if (status == std::future_status::ready)
{
//表示线程成功返回
cout << " ready " << endl;
cout << result.get() << endl;
}
else if(status == std::future_status::deferred)
{
//如果async的第一个参数设置为 std::launch::def
cout << "线程延迟执行" << endl;
cout << result.get() << endl;
}
cout << "continue.... " << endl;
return 0;
}
std::shread_future:也是类模板,get()函数只是复制数据
int mythread()
{
cout << "mythread() start" << " thread id = " << std::this_thread::get_id() << endl;
std::this_thread::sleep_for(2s);
return 5;
}
void mythread2(std::shared_future<int> &temp)
{
cout<<"mythread2() start" << " thread id = " << std::this_thread::get_id() << endl;
auto result = temp.get();
cout << "mythread2 result = " << result << endl;
return;
}
int main()
{
cout << "main id = " << std::this_thread::get_id() << endl;
std::packaged_task<int(void)> mypt(mythread);
std::thread t1(std::ref(mypt));
t1.join();
//std::future<int> result = mypt.get_future();
////std::shared_future<int> result_s(std::move(result));
//std::shared_future<int> result_s(result.share());
//shared_future的get()函数只是复制数据
shared_future<int>result_s(mypt.get_future());
std::thread t2(mythread2, std::ref(result_s));
t2.join();
cout << "continue.... " << endl;
return 0;
}
当两个线程同时修改一个全局变量时,可能会出错
int g_mycount = 0;
void mythread()
{
cout << "mythread() start" << " thread id = " << std::this_thread::get_id() << endl;
for (int i = 0; i < 10000000; i++)
{
g_mycount++;
}
return;
}
int main()
{
cout << "main id = " << std::this_thread::get_id() << endl;
thread mytobj1(mythread);
thread mytobj2(mythread);
mytobj1.join();
mytobj2.join();
cout << "g_mucount = " << g_mycount << endl;
cout << "continue.... " << endl;
return 0;
}
atomic:针对++,–,+=,&=,|=,&=,|=,^=是支持的。其它的可能不支持。
std::atomic<int> g_mycount = 0;//封装一个类型为int的对象
std::atomic<bool> g_ifend = false;//线程退出标记。防止读写乱套
void mythread()
{
while(g_ifend == false)
{
cout << "mythread() start" << " thread id = " << std::this_thread::get_id() << endl;
std::this_thread::sleep_for(2s);
}
cout << "mythread() end" << " thread id = " << std::this_thread::get_id() << endl;
return;
}
int main()
{
//原子操作:不需要用到互斥量加锁技术的多线程并发编程方式
//在多线程中,不会被打断程序的执行片段,比互斥量效率上更胜一筹。
//互斥量的加锁一般是针对一个代码段(几行代码或者更多),而原子操作针对的是一个变量。
//std::atomic来代表原子操作,是个类模板。是用来封装某个类型的值。
cout << "main id = " << std::this_thread::get_id() << endl;
thread mytobj1(mythread);
thread mytobj2(mythread);
std::this_thread::sleep_for(5s);
g_ifend = true;
mytobj1.join();
mytobj2.join();
cout << "continue.... " << endl;
return 0;
}
std::async参数详述:用来创建一个异步任务
std::launch::deferred 延迟调用,并且不创建新线程,并在.get()或.wait()调用时才执行线程函数
std::launch::async 强制创建一个线程
std::thread() 如果系统资源紧张,那么可能创建线程就会失败,那么执行thread()时整个程序可能崩溃。
std::async() 一般叫 创建一个异步任务
std::async 和 thread 不同: async有时候并不创建线程。
如果两个参数一起调用(或不带参数),那就由系统选择。
系统如何决定是 创建新线程还是同步方式运行。
std::thread创建线程的方式,如果线程返回值,你想拿到这个值也不容易。
由于系统资源限制
1)如果用thread创建的线程太多,则可能创建失败,系统报告异常,崩溃
2)如果用async,一般就不会报异常不会崩溃,因为如果系统资源紧张导致无法创建新线程的时候,
async这种不加额外参数的调用就不会创建新线程,而是后续谁调用了 .get()的线程中继续执行。
async不确定性问题解决
不加参数的async调用 std::future<int>result = std::async(mythread);
使用wait_for 解决