I'm trying to overwrite the value of the next item in the array with the value taked as argument in the function TaskCode by accessing it from its memory address. I have tried a lot of combinations, but it does not work as I am expecting.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define NUM_THREADS 5
void* TaskCode(void* argument) {
int tid = *((int*)argument); //set tid to value of thread
tid++; // go to next memory address of thread_args
tid = *((int*)argument); // set that value to the value of argument
printf("\nI have the value: \" %d \" and address: %p! \n", tid, &tid);
return NULL;
}
int main(int argc, char* argv[])
{
pthread_t threads[NUM_THREADS]; // array of 5 threads
int thread_args[NUM_THREADS +1 ]; // array of 6 integers
int rc, i;
for (i = 0; i < NUM_THREADS; ++i) {/* create all threads */
thread_args[i] = i; // set the value thread_args[i] to 0,1...,4
printf("In main: creating thread %d\n", i);
rc = pthread_create(&threads[i], NULL, TaskCode,
(void*)&thread_args[i]);
assert(0 == rc);
}
/* wait for all threads to complete */
for (i = 0; i < NUM_THREADS; ++i) {
rc = pthread_join(threads[i], NULL);
assert(0 == rc);
}
exit(EXIT_SUCCESS);
}