mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 04:44:34 +03:00
Example CP.4 (task vs thread)
This commit is contained in:
@@ -13393,8 +13393,29 @@ Application concepts are easier to reason about.
|
||||
|
||||
##### 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
|
||||
|
||||
With the exception of `async()`, the standard-library facilities are low-level, machine-oriented, threads-and-lock level.
|
||||
|
||||
Reference in New Issue
Block a user