fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. void atomic_set_bit(uint32_t *address, int bit)
  5. {
  6. __sync_fetch_and_or(address, 1 << bit);
  7. }
  8.  
  9. int main(){
  10. uint32_t bob = 0;
  11.  
  12. printf("Bob: %zu\n", bob);
  13.  
  14. for (int i = 0; i < 32; ++i) {
  15. atomic_set_bit(&bob, i);
  16. printf("Bob: %zu\n", bob);
  17. bob = 0;
  18. }
  19.  
  20. return 0;
  21. }
  22.  
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
Bob: 0
Bob: 1
Bob: 2
Bob: 4
Bob: 8
Bob: 16
Bob: 32
Bob: 64
Bob: 128
Bob: 256
Bob: 512
Bob: 1024
Bob: 2048
Bob: 4096
Bob: 8192
Bob: 16384
Bob: 32768
Bob: 65536
Bob: 131072
Bob: 262144
Bob: 524288
Bob: 1048576
Bob: 2097152
Bob: 4194304
Bob: 8388608
Bob: 16777216
Bob: 33554432
Bob: 67108864
Bob: 134217728
Bob: 268435456
Bob: 536870912
Bob: 1073741824
Bob: 2147483648