aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/multithreaded_volatile/volatile.c
blob: d4e08d6d38eced9fcbc9cdcb823b6eb1d44dea4d (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
31
32
33
#include <stdio.h>
#include <time.h>

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);
int pthread_join(pthread_t thread, void **retval);

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");
}