From 45376bb6b20adbf1bd2db0e9aea458a6fd825024 Mon Sep 17 00:00:00 2001 From: luav Date: Sun, 15 Oct 2017 10:06:01 +0200 Subject: [PATCH 1/5] 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. From f8159d26e01caedc18a889e8218be020d8bee1c8 Mon Sep 17 00:00:00 2001 From: luav Date: Sun, 15 Oct 2017 10:31:33 +0200 Subject: [PATCH 2/5] Formatting and grammer fixed --- CppCoreGuidelines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index ecf7f48..57e6b37 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -13402,7 +13402,7 @@ Application concepts are easier to reason about. void manual_publishing(std::string* msg) { - // Incapsulates thread functionality into the application task + // Encapsulates thread functionality into the application task std::thread publisher(publish, &msg); publisher.join(); } From 147f4d2dc355f6f7b03f5890f0b0a125415d641a Mon Sep 17 00:00:00 2001 From: luav Date: Sun, 15 Oct 2017 10:42:51 +0200 Subject: [PATCH 3/5] Spaces in blank lines removed --- CppCoreGuidelines.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 57e6b37..d8e7d74 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -13399,14 +13399,14 @@ Application concepts are easier to reason about. *msg = "Hello"; // ... } - + void manual_publishing(std::string* msg) { // Encapsulates 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) @@ -13415,7 +13415,7 @@ Application concepts are easier to reason about. // ... publisher.join(); } - + ##### Note With the exception of `async()`, the standard-library facilities are low-level, machine-oriented, threads-and-lock level. From 17fe8a957fd47397d502113918db992348f6707e Mon Sep 17 00:00:00 2001 From: luav Date: Mon, 16 Oct 2017 12:08:21 +0200 Subject: [PATCH 4/5] Manual non-async task removed --- CppCoreGuidelines.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index d8e7d74..d89e52b 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -13400,18 +13400,10 @@ Application concepts are easier to reason about. // ... } - void manual_publishing(std::string* msg) - { - // Encapsulates 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) + std::thread publisher(publish, &msg); // bad (less expressive and more error-prone) + auto pubtask = std::async(publish, &msg); // OK // ... publisher.join(); } From 7a5ab334a09766ed8339ae140e98b50e184c0101 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 23 Oct 2017 19:24:53 +0100 Subject: [PATCH 5/5] Update CppCoreGuidelines.md --- CppCoreGuidelines.md | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index d89e52b..ffccdda 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -13393,17 +13393,10 @@ Application concepts are easier to reason about. ##### Example - void publish(std::string* msg) - { - // ... - *msg = "Hello"; - // ... - } - void some_fun() { - std::string msg; - std::thread publisher(publish, &msg); // bad (less expressive and more error-prone) - auto pubtask = std::async(publish, &msg); // OK + std::string msg, msg2; + std::thread publisher([&] { msg = "Hello"; }); // bad (less expressive and more error-prone) + auto pubtask = std::async([&] { msg2 = "Hello"; }); // OK // ... publisher.join(); }