int doAsyncWork();
std::thread t(doAsyncWork);
auto fut = std::async(doAsyncWork);
auto fut1 = std::async(f);
==>等价于
auto fut2 = std::async(std::launch::async | std::launch::deferred, f);
using namespace std::literals;
void f()
{
std::this_thread::sleep_for(1s);
}
auto fut = std::async(f);
//函数f有可能一直没有被执行,那么就会一直卡在循环的判断上,这
//种情况在开发和单元测试中一般不会出现,但是在高压负载下就会出现
while(fut.wait_for(100ms) != std::future_status::ready)
{
...
}
auto fut = std::async(f);
if( fut.wait_for(0s) == std::future_status::deferred)
{
... // 通过get或者wait来同步调用函数f
}
else
{
while(fut.wait_for(100ms) != std::future_status::ready)
{
...//并发执行其他任务
}
...
}
constexpr auto tenMillion = 10000000;
bool doWork(std::function<bool(int)> filter,
int maxVal = tenMillion)
{
std::vector<int> goodVals;
std::thread t( [&filter, maxVal, &goodVals] {
for(auto i = 0; i <= maxVal ; ++i)
{
if ( filter(i))
goodVals.push_back(i);
}
});
auto nh = t.native_handle();
...
if(conditionAreSatisfied()) {
t.join();
performComputation(goodVals);
return true;
}
return false; // thread对象没有被join!!!
}
class ThreadRAII {
public:
enum class DtorAction { join, detach };
ThreadRAII(std::thread&& t, DtorAction a):
action(a), t(std::move(t)) {}
~ThreadRAII()
{
if(t.joinable()) {
if (action == DtorAction::join){
t.join();
} else {
t.detach();
}
}
}
ThreadRAII(ThreadRAII&&) = default;
ThreadRAII& operator=(ThreadRAII&&) = default;
std::thread& get() { return t;}
private:
DtorAction action;
std::thread t;
};
int calcValue();
std::packaged_task<int()> pt(calcValue);
// fut会正常析构,因为不满组上面的条件1
auto fut = pt.get_future();
{
std::packaged_task<int()> pt(calcValue);
auto fut = pt.get_future();
std::thread t(std::move(pt));
...
}
std::condition_variable cv;
std::mutex m;
// task 1
{
... // detect event
cv.notify_one();
}
//task 2
{
std::unique_lock<std::mutex> lk(m);
cv.wait(lk);
... // react to event
}
std::atomic<bool> flag(false);
//task 1
{
... // detect event
flag = true;
}
//task 2
{
...
while(!flag); // wait for event
...
}
std::condition_variable cv;
std::mutex m;
bool flag(false);
//task 1
{
... // detect event
{
std::lock_guard<std::mutex> g(m);
flag = true;
}
cv.notify_one();
}
//task 2
{
...
{
std::unique_lock<std::mutex> lk(m);
// use lambda to avoid spurious wakeups
cv.wait(lk, [] {return flag; });
... // react to event
}
...
}
std::promise<void> p;
// task 1
{
... // detect event
p.set_value(); // tell reacting task
}
// task 2
{
...
// wait on future corresponding to p
p.get_future().wait();
... // react to event
}
std::promise<void> p;
void react();
void detect()
{
std::thread t([] {
p.get_future().wait();
react();
});
... // do something else before starting thread
p.set_value();
...
t.join();
}
std::promise<void> p;
void detect()
{
auto sf = p.get_future().share();
for(int i = 0; i < threadsToRun; ++i)
{
vt.emplace_back([sf] { sf.wait(); react(); });
}
...
p.set_value();
...
for(auto& t : vt){
t.join();
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。