Created
August 18, 2019 21:05
-
-
Save igoratf/59fcda546db04a2a3c33cbc2c7e25ad1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <assert.h> | |
#include <pthread.h> | |
long int counter = 0; | |
void* run (void* args) { | |
int my_id; | |
long int j; | |
my_id = (int) args; | |
for (j=0;j<1000000; j++) { | |
counter = counter + 1; | |
} | |
printf("my_id=%d j=%ld counter=%ld\n", my_id, j, counter); | |
pthread_exit(my_id); | |
} | |
int main (int argc, char *argv[]) { | |
int i; | |
pthread_t pthreads[3]; | |
for (i=0; i<3;i++) { | |
pthread_create (&pthreads[i], NULL, &run, (void*) i); | |
} | |
for (i=0; i<3; i++) { | |
pthread_join(pthreads[i], NULL); | |
} | |
printf("counter=%ld\n", counter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment