@@ -10376,13 +10376,13 @@ Local static variables are a common source of data races.
1037610376
1037710377 void f(fstream& fs, regex pat)
1037810378 {
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};
1038210382 // ...
10383- auto h1 = async([&]{ sort(par,s); }); // spawn a task to sort
10383+ auto h1 = async([&]{ sort(par, s); }); // spawn a task to sort
1038410384 // ...
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
1038610386 // ...
1038710387 }
1038810388
@@ -10399,7 +10399,7 @@ Not all data races are as easy to spot as this one.
1039910399
1040010400 if (val < 5) {
1040110401 // ... other thread can change val here ...
10402- switch(val) {
10402+ switch (val) {
1040310403 case 0: // ...
1040410404 case 1: // ...
1040510405 case 2: // ...
@@ -10764,11 +10764,11 @@ If a `thread` is detached, we can safely pass pointers to static and free store
1076410764 void some_fct(int* p)
1076510765 {
1076610766 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
1077010770 auto q = make_unique<int>(99);
10771- std::thread t3(f,q.get()); // bad
10771+ std::thread t3(f, q.get()); // bad
1077210772 // ...
1077310773 t0.detach();
1077410774 t1.detach();
@@ -11033,7 +11033,7 @@ Instead, we could have a set of pre-created worker threads processing the messag
1103311033
1103411034 void master(istream& is)
1103511035 {
11036- for (Message m; is>> m; )
11036+ for (Message m; is >> m; )
1103711037 work.put(n);
1103811038 }
1103911039
@@ -11119,8 +11119,8 @@ Here, if some other `thread` consumes `thread1`'s notification, `thread2` can wa
1111911119 void Sync_queue<T>::get(T& val)
1112011120 {
1112111121 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();
1112411124 q.pop_front();
1112511125 }
1112611126
@@ -11336,13 +11336,13 @@ It's error-prone and requires expert level knowledge of language features, machi
1133611336
1133711337 extern atomic<Link*> head; // the shared head of a linked list
1133811338
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
1134111341
1134211342 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
1134611346
1134711347Spot the bug.
1134811348It would be really hard to find through testing.
0 commit comments