File tree Expand file tree Collapse file tree 1 file changed +10
-15
lines changed Expand file tree Collapse file tree 1 file changed +10
-15
lines changed Original file line number Diff line number Diff line change @@ -133,26 +133,21 @@ namespace false_sharing
133133
134134 namespace  false_sharing_resolved 
135135 {
136-  //  aligned type with the same size of cache line
137-  struct  alignas (64 ) aligned_type
138-  {
139-  std::atomic<int > val;
140-  };
141- 
142136 int  test ()
143137 {
144-  aligned_type a; a.val  = 0 ; //  &a: 0x...4ff240
145-  aligned_type b; b.val  = 0 ; //  &b: 0x...4ff280
146-  aligned_type c; c.val  = 0 ; //  &c: 0x...4ff2c0
147-  aligned_type d; d.val  = 0 ; //  &d: 0x...4ff300
138+  //  aligned type with the same size of cache line
139+  alignas (64 ) std::atomic<int > a{}; //  &a: 0x...4ff240
140+  alignas (64 ) std::atomic<int > b{}; //  &b: 0x...4ff280
141+  alignas (64 ) std::atomic<int > c{}; //  &c: 0x...4ff2c0
142+  alignas (64 ) std::atomic<int > d{}; //  &d: 0x...4ff300
148143
149-  std::thread t1 ([&a] { work (a. val ); });
150-  std::thread t2 ([&b] { work (b. val ); });
151-  std::thread t3 ([&c] { work (c. val ); });
152-  std::thread t4 ([&d] { work (d. val ); });
144+  std::thread t1 ([&a] { work (a); });
145+  std::thread t2 ([&b] { work (b); });
146+  std::thread t3 ([&c] { work (c); });
147+  std::thread t4 ([&d] { work (d); });
153148
154149 t1.join (); t2.join (); t3.join (); t4.join ();
155-  return  a. val  + b. val  + c. val  + d. val ;
150+  return  a + b + c + d;
156151 }
157152 }
158153
                         You can’t perform that action at this time. 
           
                  
0 commit comments