I want to create 2 Threads, which are in use of a global variable, my code:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
int var = 0; //
pthread_t threads[2];
void* function(){
if(var % 2==0){
var +=2;
}
printf("Addresse %d with var %d\n", &var, var);
}
int main() {
for(int i = 0; i < 2; i++){
pthread_create(&threads[i], NULL, &function, NULL);
}
pthread_exit(NULL);
}
I created 2 threads with the for loop. I want to let both threads use the global variable. One shall increment 2, the other thread shall multiply 2. I used printf to see, that both use same addresse, but not have same output. How do I let each of the threads to different tasks?