void *count(void* null){ printf("New thread created\n"); // do something }
intmain(){ // declare an array of 4 threads pthread_t handlers[THREADS];
for (intptr_t i = 0; i < THREADS; i++) { int res = pthread_create(&handlers[i], NULL, count, NULL); assert(!res); }
for (int i = 0; i < THREADS; i++) { int res = pthread_join(handlers[i], NULL); assert(!res); }
if (counter != RUNS * THREADS) { printf("Didn't count so well. :/, found %d\n", counter); } else { printf("Counted up to %d.\n", counter); } }
Lock
1 2 3 4 5 6 7 8 9 10 11
#include"lock.h"
void* count(void* null){ for (int r = 0; r < RUNS; r++) { lock_acquire(&lock); counter++; lock_release(&lock); }
returnNULL; }
Better version: use lock_wait(&lock) and lock_wake_up(&lock) to prevent bucy wait, but rely on a notification.
Atomic variable
Atomic variables (~atomic registers) do not enforce atomic operation blocks.
Need to use atomic operation.
1 2 3 4 5 6 7 8 9
staticatomic_int counter = 0;
void* count(void* null){ for (int r = 0; r < RUNS; r++) { atomic_fetch_add(&counter, 1); }
returnNULL; }
Another example
1 2 3 4
int expected = 0; if (atomic_compare_exchange_strong(&leader[r], &expected, tid)) { atomic_fetch_add(&nb_leaders[r], 1); // used to check correctness }
Debug
当有include .h时,报错
1
clang: error: linker command failed with exit code 1