c++ unnamed programming (addendum)
1 minute read
In a previous unnamed programming post I've mentioned how the comma operator can be used to evade naming temporaries.
I've come to realize that can also be used for joining async
tasks:
#include <mutex>
#include <future>
#include <iostream>
int main() {
using namespace std;
mutex m;
async(launch::async, [&m] { lock_guard{m}, cout << "hello"; }),
async(launch::async, [&m] { lock_guard{m}, cout << "world"; });
}
If .get()
is not called on the future
returned by std::async
, it's
well-known that the future
's destructor will block until the task is
finished. In the above example two temporary future
s are created, and they're
"joined" with a comma operator. Both are executed in parallel and nothing
afterwards will execute until both temporaries cease to exist, which means
until both tasks are finished.
Notice how there's just one named variable in this code, there was no need to
name lock_guard
s nor future
s. 😂
Update
C++20 adds jthread
s, so the above could be simplified to:
#include <mutex>
#include <thread>
#include <iostream>
int main() {
using namespace std;
mutex m;
jthread([&m] { lock_guard{m}, cout << "hello"; }),
jthread([&m] { lock_guard{m}, cout << "world"; });
}
Spotted a mistake in this article? Why not suggest an edit!