From 45376bb6b20adbf1bd2db0e9aea458a6fd825024 Mon Sep 17 00:00:00 2001 From: luav Date: Sun, 15 Oct 2017 10:06:01 +0200 Subject: [PATCH] Example CP.4 (task vs thread) --- CppCoreGuidelines.md | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 5ef36ae..ecf7f48 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -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.