Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- volatile char toggle = 1;
- void *writer(void *unused) {
- for (;;) {
- toggle = 1;
- toggle = 2;
- }
- return unused;
- }
- int main() {
- pthread_t writer_thread;
- pthread_create(&writer_thread, NULL, &writer, NULL);
- long ones = 0, twos = 0;
- for (int i = 0; i < 100000; i++) {
- if (toggle == 1) {
- ones++;
- } else {
- twos++;
- }
- }
- printf("Ones: %ld; twos: %ld\n", ones, twos);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement