Skip to content

Commit 45376bb

Browse files
committed
Example CP.4 (task vs thread)
1 parent e251862 commit 45376bb

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

CppCoreGuidelines.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13393,8 +13393,29 @@ Application concepts are easier to reason about.
1339313393

1339413394
##### Example
1339513395

13396-
???
13397-
13396+
void publish(std::string* msg)
13397+
{
13398+
// ...
13399+
*msg = "Hello";
13400+
// ...
13401+
}
13402+
13403+
void manual_publishing(std::string* msg)
13404+
{
13405+
// Incapsulates thread functionality into the application task
13406+
std::thread publisher(publish, &msg);
13407+
publisher.join();
13408+
}
13409+
13410+
void some_fun() {
13411+
std::string msg;
13412+
std::thread publisher(publish, &msg); // bad (less expressive and more error-prone)
13413+
auto pubtask = std::sync(publish, &msg); // OK
13414+
manual_publishing(&msg); // OK (manually crafted task)
13415+
// ...
13416+
publisher.join();
13417+
}
13418+
1339813419
##### Note
1339913420

1340013421
With the exception of `async()`, the standard-library facilities are low-level, machine-oriented, threads-and-lock level.

0 commit comments

Comments
 (0)