@@ -10376,13 +10376,13 @@ Local static variables are a common source of data races.
10376
10376
10377
10377
void f(fstream& fs, regex pat)
10378
10378
{
10379
- array<double,max> buf;
10380
- int sz = read_vec(fs,buf,max); // read from fs into buf
10381
- gsl::span<double> s {buf,max};
10379
+ array<double, max> buf;
10380
+ int sz = read_vec(fs, buf, max); // read from fs into buf
10381
+ gsl::span<double> s {buf, max};
10382
10382
// ...
10383
- auto h1 = async([&]{ sort(par,s); }); // spawn a task to sort
10383
+ auto h1 = async([&]{ sort(par, s); }); // spawn a task to sort
10384
10384
// ...
10385
- auto h2 = async([&]{ return find_all(buf,sz,pat); }); // span a task to find matches
10385
+ auto h2 = async([&]{ return find_all(buf, sz, pat); }); // span a task to find matches
10386
10386
// ...
10387
10387
}
10388
10388
@@ -10399,7 +10399,7 @@ Not all data races are as easy to spot as this one.
10399
10399
10400
10400
if (val < 5) {
10401
10401
// ... other thread can change val here ...
10402
- switch(val) {
10402
+ switch (val) {
10403
10403
case 0: // ...
10404
10404
case 1: // ...
10405
10405
case 2: // ...
@@ -10764,11 +10764,11 @@ If a `thread` is detached, we can safely pass pointers to static and free store
10764
10764
void some_fct(int* p)
10765
10765
{
10766
10766
int x = 77;
10767
- std::thread t0(f,&x); // bad
10768
- std::thread t1(f,p); // bad
10769
- std::thread t2(f,&glob); // OK
10767
+ std::thread t0(f, &x); // bad
10768
+ std::thread t1(f, p); // bad
10769
+ std::thread t2(f, &glob); // OK
10770
10770
auto q = make_unique<int>(99);
10771
- std::thread t3(f,q.get()); // bad
10771
+ std::thread t3(f, q.get()); // bad
10772
10772
// ...
10773
10773
t0.detach();
10774
10774
t1.detach();
@@ -11033,7 +11033,7 @@ Instead, we could have a set of pre-created worker threads processing the messag
11033
11033
11034
11034
void master(istream& is)
11035
11035
{
11036
- for (Message m; is>> m; )
11036
+ for (Message m; is >> m; )
11037
11037
work.put(n);
11038
11038
}
11039
11039
@@ -11119,8 +11119,8 @@ Here, if some other `thread` consumes `thread1`'s notification, `thread2` can wa
11119
11119
void Sync_queue<T>::get(T& val)
11120
11120
{
11121
11121
unique_lock<mutex> lck(mtx);
11122
- cond.wait(lck,[this]{ return !q.empty(); }); // prevent spurious wakeup
11123
- val= q.front();
11122
+ cond.wait(lck, [this]{ return !q.empty(); }); // prevent spurious wakeup
11123
+ val = q.front();
11124
11124
q.pop_front();
11125
11125
}
11126
11126
@@ -11336,13 +11336,13 @@ It's error-prone and requires expert level knowledge of language features, machi
11336
11336
11337
11337
extern atomic<Link*> head; // the shared head of a linked list
11338
11338
11339
- Link* nh = new Link(data,nullptr); // make a link ready for insertion
11340
- Link* h = head.load(); // read the shared head of the list
11339
+ Link* nh = new Link(data, nullptr); // make a link ready for insertion
11340
+ Link* h = head.load(); // read the shared head of the list
11341
11341
11342
11342
do {
11343
- if (h->data<= data) break; // if so, insert elsewhere
11344
- nh->next = h; // next element is the previous head
11345
- } while (!head.compare_exchange_weak(h,nh)); // write nh to head or to h
11343
+ if (h->data <= data) break; // if so, insert elsewhere
11344
+ nh->next = h; // next element is the previous head
11345
+ } while (!head.compare_exchange_weak(h, nh)); // write nh to head or to h
11346
11346
11347
11347
Spot the bug.
11348
11348
It would be really hard to find through testing.
0 commit comments