Closed MisterDo closed 9 months ago
I realize this was posted over two years ago...just trying to help the maintainers close out some issues.
The result you are getting is due to a bug in your code and not with C-Thread-Pool. You are using the same task_args
struct copy for both threads, rather than using separate memory for each thread. The first thread task starts before the start
and end
members have been reset to 10 and 20, respectively, , which is why only a couple of the num
elements <10 are set to nonzero values.
You can easily verify this by replacing your loop with the following:
task_args args[2];
for(int i=0;i<thread_num;i++){
int start = i*range;
int end = (i+1)*range;
if(i==thread_num){
end = nums_len;
}
args[i].nums = nums;
args[i].start = start;
args[i].end = end;
thpool_add_work(thpool,task,(void *)&args[i]);
}
Now, since each thread uses a different task_args
struct, the program outputs the expected result.
The some elements of the array are not assigned values? nums[0]=0 nums[1]=0 nums[2]=0 nums[3]=0 nums[4]=0 nums[5]=0 nums[6]=0 nums[7]=0 nums[8]=8 nums[9]=9 nums[10]=0 nums[11]=0 nums[12]=12 nums[13]=13 nums[14]=0 nums[15]=0 nums[16]=16 nums[17]=17 nums[18]=18 nums[19]=19 ......