Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtok function thread safety

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.

like image 419
Steveng Avatar asked Oct 27 '10 08:10

Steveng


People also ask

What does the function strtok do?

The strtok() function reads string1 as a series of zero or more tokens, and string2 as the set of characters serving as delimiters of the tokens in string1. The tokens in string1 can be separated by one or more of the delimiters from string2.

How does strtok change the string?

The strtok() function parses the string up to the first instance of the delimiter character, replaces the character in place with a null byte ( '\0' ), and returns the address of the first character in the token. Subsequent calls to strtok() begin parsing immediately after the most recently placed null character.

Does strtok affect the original string?

strtok() doesn't create a new string and return it; it returns a pointer to the token within the string you pass as argument to strtok() . Therefore the original string gets affected. strtok() breaks the string means it replaces the delimiter character with NULL and returns a pointer to the beginning of that token.

What is strtok () in C++?

The strtok() function is used in tokenizing a string based on a delimiter. It is present in the header file “string. h” and returns a pointer to the next token if present, if the next token is not present it returns NULL. To get all the tokens the idea is to call this function in a loop.


2 Answers

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

like image 77
Puppe Avatar answered Sep 28 '22 13:09

Puppe


strtok() is not MT-safe because it stores some intermediate variables globally and reuse them at each call (see you don't have to pass again the string each time you call strtok()). You can have a look at the man pages of methods you are using and it is always indicated at the end if it is MT-safe or not.

When a method is not MT-safe (multi-thread safe or reentrant), you should look for same method with suffix _r meaning reentrand. In your example, strtok_r() as suggested in the other answer.

like image 36
Benoit Thiery Avatar answered Sep 28 '22 14:09

Benoit Thiery