Skip to content

Commit 757737e

Browse files
Boris Smidttkruse
authored andcommitted
changed tabs into 4 spaces
1 parent e8dea38 commit 757737e

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

CppCoreGuidelines.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10786,27 +10786,27 @@ A `thread` that has not been `detach()`ed when it is destroyed terminates the pr
1078610786
void f() { std::cout << "Hello "; }
1078710787

1078810788
struct F {
10789-
void operator()() { std::cout << "parallel world "; }
10789+
void operator()() { std::cout << "parallel world "; }
1079010790
};
1079110791

1079210792
int main()
1079310793
{
10794-
std::thread t1{f}; // f() executes in separate thread
10795-
std::thread t2{F()}; // F()() executes in separate thread
10794+
std::thread t1{f}; // f() executes in separate thread
10795+
std::thread t2{F()}; // F()() executes in separate thread
1079610796
} // spot the bugs
1079710797

1079810798
##### Example
1079910799

1080010800
void f() { std::cout << "Hello "; }
1080110801

1080210802
struct F {
10803-
void operator()() { std::cout << "parallel world "; }
10803+
void operator()() { std::cout << "parallel world "; }
1080410804
};
1080510805

1080610806
int main()
1080710807
{
10808-
std::thread t1{f}; // f() executes in separate thread
10809-
std::thread t2{F()}; // F()() executes in separate thread
10808+
std::thread t1{f}; // f() executes in separate thread
10809+
std::thread t2{F()}; // F()() executes in separate thread
1081010810

1081110811
t1.join();
1081210812
t2.join();
@@ -10992,20 +10992,20 @@ A `wait` without a condition can miss a wakeup or wake up simply to find that th
1099210992

1099310993
void thread1()
1099410994
{
10995-
while (true) {
10996-
// do some work ...
10997-
std::unique_lock<std::mutex> lock(mx);
10998-
cv.notify_one(); // wake other thread
10999-
}
10995+
while (true) {
10996+
// do some work ...
10997+
std::unique_lock<std::mutex> lock(mx);
10998+
cv.notify_one(); // wake other thread
10999+
}
1100011000
}
1100111001

1100211002
void thread2()
1100311003
{
11004-
while (true) {
11005-
std::unique_lock<std::mutex> lock(mx);
11006-
cv.wait(lock); // might block forever
11007-
// do work ...
11008-
}
11004+
while (true) {
11005+
std::unique_lock<std::mutex> lock(mx);
11006+
cv.wait(lock); // might block forever
11007+
// do work ...
11008+
}
1100911009
}
1101011010

1101111011
Here, if some other `thread` consumes `thread1`'s notification, `thread2` can wait forever.
@@ -11015,30 +11015,30 @@ Here, if some other `thread` consumes `thread1`'s notification, `thread2` can wa
1101511015
template<typename T>
1101611016
class Sync_queue {
1101711017
public:
11018-
void put(const T& val);
11019-
void put(T&& val);
11020-
void get(T& val);
11018+
void put(const T& val);
11019+
void put(T&& val);
11020+
void get(T& val);
1102111021
private:
11022-
mutex mtx;
11023-
condition_variable cond; // this controls access
11024-
list<T> q;
11022+
mutex mtx;
11023+
condition_variable cond; // this controls access
11024+
list<T> q;
1102511025
};
1102611026

1102711027
template<typename T>
1102811028
void Sync_queue<T>::put(const T& val)
1102911029
{
11030-
lock_guard<mutex> lck(mtx);
11031-
q.push_back(val);
11032-
cond.notify_one();
11030+
lock_guard<mutex> lck(mtx);
11031+
q.push_back(val);
11032+
cond.notify_one();
1103311033
}
1103411034

1103511035
template<typename T>
1103611036
void Sync_queue<T>::get(T& val)
1103711037
{
11038-
unique_lock<mutex> lck(mtx);
11039-
cond.wait(lck,[this]{ return !q.empty(); }); // prevent spurious wakeup
11040-
val=q.front();
11041-
q.pop_front();
11038+
unique_lock<mutex> lck(mtx);
11039+
cond.wait(lck,[this]{ return !q.empty(); }); // prevent spurious wakeup
11040+
val=q.front();
11041+
q.pop_front();
1104211042
}
1104311043

1104411044
Now if the queue is empty when a thread executing `get()` wakes up (e.g., because another thread has gotton to `get()` before it),
@@ -11251,15 +11251,15 @@ It's error-prone and requires expert level knowledge of language features, machi
1125111251

1125211252
##### Example, bad
1125311253

11254-
extern atomic<Link*> head; // the shared head of a linked list
11254+
extern atomic<Link*> head; // the shared head of a linked list
1125511255

11256-
Link* nh = new Link(data,nullptr); // make a link ready for insertion
11257-
Link* h = head.load(); // read the shared head of the list
11256+
Link* nh = new Link(data,nullptr); // make a link ready for insertion
11257+
Link* h = head.load(); // read the shared head of the list
1125811258

1125911259
do {
11260-
if (h->data<=data) break; // if so, insert elsewhere
11261-
nh->next = h; // next element is the previous head
11262-
} while (!head.compare_exchange_weak(h,nh)); // write nh to head or to h
11260+
if (h->data<=data) break; // if so, insert elsewhere
11261+
nh->next = h; // next element is the previous head
11262+
} while (!head.compare_exchange_weak(h,nh)); // write nh to head or to h
1126311263

1126411264
Spot the bug.
1126511265
It would be really hard to find through testing.

0 commit comments

Comments
 (0)