aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/multithreaded_volatile/volatile.c
blob: f8ffee2db26f30e0f1b7e557a3bffcc5ea3b4873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <time.h>
#include <pthread.h>

typedef unsigned data;

static inline data powM(data x, unsigned e) {
  data y = 1;
  for(unsigned i=0; i<e; i++) {
    y = (y * x) % 65537;
  }
  return y;
}

void* second_thread_entry(void *ptr) {
  *((volatile data*) ptr) = powM(3, 65536);
  return NULL;
}

int main() {
  pthread_t second_thread_id;
  volatile data value;
  pthread_create(&second_thread_id, NULL,
                 second_thread_entry, (void*) &value);
  value = 69;
  data correct = powM(3, 65536*2);
  data read = value;
  pthread_join(second_thread_id, NULL);
  printf("%u %u %s\n", read, correct, read == correct ? "OK" : "FAIL");
}