Co-founder @ SMERGERS and

Recently, I stumbled upon this method "strtok" in C which is used to split a string based on a delimiter. This is how it is used:

1. If str is a string, start from the beginning of the new string.
2. If str is NULL, start from the place where it had left in the last call.

What this implies is, you simply cannot split two strings in parallel. Imagine the following scenario. Two threads executing simultaneously, one of them is splitting the string str1 and another one is splitting the string str2.

let us consider the following order for execution:

strtok(str1, ",") // thread 1
strtok(str2, ",") // thread 2

token = strtok(NULL, delim); // thread 1
token = strtok(NULL, delim); // thread 2

We will get only the first token from str1 since the buffer of strtok is overwritten the moment we call strtok(str2, ",") in the second thread. What's worse is, strtok alters the input string in order to keep track of the position of the next token. I don't know why this was so horribly designed, anyone?

Update: Thanks to Vijay and Debarshi for pointing out strtok_r which is thread safe. I wasn't aware of this.. Thanks guys..

blog comments powered by Disqus