File: atomic_sync_synchronize.c

package info (click to toggle)
chibicc 1.0.23.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,832 kB
  • sloc: ansic: 62,911; sh: 275; makefile: 92
file content (22 lines) | stat: -rw-r--r-- 462 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <pthread.h>
#include "test.h"

int shared_value = 0;

void *thread_func(void *arg) {
    __sync_synchronize(); // Memory barrier
    shared_value = 42;
    __sync_synchronize(); // Memory barrier
    return NULL;
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, thread_func, NULL);
    pthread_join(thread, NULL);
    printf("Shared value: %d\n", shared_value); // Shared value: 42
    ASSERT(42, shared_value);
    return 0;
}