Example CP.4 (task vs thread)

This commit is contained in:
luav 2017-10-15 10:06:01 +02:00
parent e251862408
commit 45376bb6b2

View File

@ -13393,7 +13393,28 @@ Application concepts are easier to reason about.
##### Example ##### Example
??? void publish(std::string* msg)
{
// ...
*msg = "Hello";
// ...
}
void manual_publishing(std::string* msg)
{
// Incapsulates thread functionality into the application task
std::thread publisher(publish, &msg);
publisher.join();
}
void some_fun() {
std::string msg;
std::thread publisher(publish, &msg); // bad (less expressive and more error-prone)
auto pubtask = std::sync(publish, &msg); // OK
manual_publishing(&msg); // OK (manually crafted task)
// ...
publisher.join();
}
##### Note ##### Note