Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <stdlib.h>
- long counter = 0;
- pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
- void* incrementCounter(void* iter) {
- for (int i = 0; i < *(int*)iter; i++) {
- // pthread_mutex_lock(&mutex1);
- counter++;
- // pthread_mutex_unlock(&mutex1);
- }
- }
- int main(int argc, char** argv) {
- if (argc != 3) return 1;
- int n = atoi(argv[1]);
- int iter = atoi(argv[2]);
- pthread_t threads[n];
- for (int i = 0; i < n; i++) {
- pthread_create(&threads[i], NULL, incrementCounter, &iter);
- }
- for (int i = 0; i < n; i++) {
- pthread_join(threads[i], NULL);
- }
- printf("%ld\n", counter);
- }
Advertisement
Add Comment
Please, Sign In to add comment