Spinlocks
This commit is contained in:
parent
080928d3fd
commit
33d55b2cd2
3 changed files with 111 additions and 0 deletions
31
spinlocks/main.c
Normal file
31
spinlocks/main.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include "spinlock.h"
|
||||
|
||||
spinlock_t lock;
|
||||
int counter = 0;
|
||||
|
||||
void *worker(void *arg) {
|
||||
printf("Hello from %s!\n", (char *)arg);
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
acquire(&lock);
|
||||
counter++;
|
||||
release(&lock);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
pthread_t thread1, thread2;
|
||||
|
||||
initlock(&lock);
|
||||
|
||||
pthread_create(&thread1, NULL, worker, "Thread1");
|
||||
pthread_create(&thread2, NULL, worker, "Thread2");
|
||||
|
||||
pthread_join(thread1, NULL);
|
||||
pthread_join(thread2, NULL);
|
||||
|
||||
printf("Counter value: %d\n", counter);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue