strtok function thread safety

Steveng picture Steveng · Oct 27, 2010 · Viewed 12.8k times · Source

I have been spending some time in debugging a programme which gives segmentation fault. The bug is quite indeterministic and intermittent, which is annoying. I narrowed it down to the calling of strtok(). I suspect that it is the calling of strtok() to split string in two different threads that causes the segmentation fault. Can I call strtok() in two different threads?

Thanks.

Answer

Puppe picture Puppe · Oct 27, 2010

strtok() is not reentrant so it should not be used from threaded applications, use strtok_r() instead.