Skip to content

Commit

Permalink
Extract atomic-gcc-norace example from klib
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Sep 20, 2023
1 parent cd70201 commit ae8adff
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/regression/03-practical/46-atomic-gcc-norace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Race-free due to GCC atomic operation.
// Extracted from concrat/klib.
#include <stdlib.h>
#include <pthread.h>
#include <goblint.h>
extern int __VERIFIER_nondet_int();

int data;

void *thread(void *arg) {
__sync_fetch_and_add(&data, 1); // NORACE
return NULL;
}

int main() {
int threads_total = __VERIFIER_nondet_int();
__goblint_assume(threads_total >= 0);

pthread_t *tids = malloc(threads_total * sizeof(pthread_t));

// create threads
for (int i = 0; i < threads_total; i++) {
pthread_create(&tids[i], NULL, &thread, NULL); // may fail but doesn't matter
}

// join threads
for (int i = 0; i < threads_total; i++) {
pthread_join(tids[i], NULL);
}

free(tids);

return 0;
}

0 comments on commit ae8adff

Please sign in to comment.