I am writing a pthread program now. From what I experienced in C#, I think there might be a race condition in the creation of a thread.
for (i = 0; i < 10; i++)
{
pthread_create(threads[i], NULL, &do_something, (void*)&data[i]);
}
Is it possible that local variable i is updated before the new thread is created? Therefore wrong data or threads entry in the arrays may be passed to the do_something function? At least in C# if I use Task.Factory.StartNew(), this is a big problem.
Thanks in advance.
Once
pthread_createreturns everything that is passed by value will have been copied successfully, in fact the use ofiis all evaluated prior to even entering thepthread_createfunction at all. The pointer itself is passed by value here too for the void* argument.The potential to create a race condition exists for example if you were passing
iitself by reference (or pointer) and using that to access an array inside the start routine. This is not the case in this example though.