User Tools

Site Tools


os_cp:find_out_what_is_going_on2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

os_cp:find_out_what_is_going_on2 [2024/04/17 23:33]
jkonczak utworzono
os_cp:find_out_what_is_going_on2 [2026/04/28 00:29] (current)
jkonczak
Line 22: Line 22:
 volatile char *const a = data + 64; volatile char *const a = data + 64;
 volatile char *const b = data + 64 * 2; volatile char *const b = data + 64 * 2;
-atomic_char vA, vB, control;+atomic_char vA, vB, barrier;
  
 void *th1(void *arg) { void *th1(void *arg) {
-  ​control++; +  ​barrier++; 
-  while (control ​!= 2);+  while (barrier ​!= 2);
  
   *b = 1;   *b = 1;
Line 35: Line 35:
  
 void *th2(void *arg) { void *th2(void *arg) {
-  ​control++; +  ​barrier++; 
-  while (control ​!= 2); +  while (barrier ​!= 2);
- +
-  volatile int dummy = 44; +
-  while (dummy--);+
  
   *a = 1;   *a = 1;
Line 58: Line 55:
 </​code>​ </​code>​
 <​html></​div></​html>​ <​html></​div></​html>​
 +
 +++++ C++ version of the code |
 +Compile with: \\
 +<code bash>g++ -g -O0 sb.cpp -o sb</​code>​
 +
 +<​html><​div style="​line-height:​1em"></​html>​
 +<code cpp sb.cpp>
 +#include <​cstdio>​
 +#include <​future>​
 +#include <​atomic>​
 +
 +volatile char data[64 * 3];
 +volatile char &a = data[64];
 +volatile char &b = data[64*2];
 +std::​atomic<​int_fast8_t>​ barrier;
 +
 +char th1() {
 +    char vA;
 +
 +    barrier++;
 +    while (barrier != 2);
 +
 +    b = 1;
 +    vA = a;
 +
 +    return vA;
 +}
 +    ​
 +char th2() {
 +    char vB;
 +
 +    barrier++;
 +    while (barrier != 2);
 +
 +    a = 1;
 +    vB = b;
 +
 +    return vB;
 +}
 +
 +int main() {
 +    auto a1 = std::​async(th1);​
 +    auto a2 = std::​async(th2);​
 +
 +    char vA = a1.get();
 +    char vB = a2.get();
 +
 +    printf("​%d %d\n", vA, vB);
 +    return 0;
 +}
 +</​code>​
 +<​html></​div></​html>​
 +++++
  
  
os_cp/find_out_what_is_going_on2.txt · Last modified: 2026/04/28 00:29 by jkonczak