开发者

Is there a potential race condition in pthread_create()?

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_create returns everything that is passed by value will have been copied successfully, in fact the use of i is all evaluated prior to even entering the pthread_create function 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 i itself by reference (or pointer) and using that to access an array inside the start routine. This is not the case in this example though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜