I’m trying to do some simple things in C and I’m confused.
The program is simple, the main function is handling thread according to a job que struct. Its opening up to 4 thread at a time. Around 300 threads till the end. The thread function is always the same but the args are different.
The hole code is bit long to paste here so I will paste some parts.
thread is opening as with the following parameter:
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&pth, &attr, dothejob, (void *) varis);
the thread called function
void *dothejob(void * varis){
unsigned char * arr1;
arr1 = (unsigned char*) calloc(3000000, sizeof (unsigned char));
unsigned char * arr2;
arr2 = (unsigned char*) calloc(3000000, sizeof (unsigned char));
// doing some calculations and comparisons and stuff
unsigned int topten[10];
// <---- here topten has some values from previous threads, but why ?
// picking top ten and putting it in the var topten[
free(arr1);
free(arr2);
pthread_detach(pthread_self());
}
If someone knows it please help me. Thank you in advance.
When you write this inside a function:
The array values are not initialized to
0. They contain whatever happens to be in memory at locationtoptenpoints to. It is undefined behaviour to use values from yourtoptenarray without writing your own new values into the array first. If you want your array to be filled with0s you should initialize it like so: