That's a picture of me in Baldwin Hills, Santa Monica
Bangalore based engineer currently helping SMEs grow via SMERGERS. Interested in Programming, Design, Startups and a few other things. MS-CS @USC, Los Angeles, BE @BMSCE.
I can be reached on
krishna [at] krishnabharadwaj [dot] info
Here are some of my other online profiles
Powered by Python and Django
kb
Geek, Programmer, FOSS Enthusiast, MS CS @USC. Loves Python / Django. Co-founder @ SMERGERS, Previously at Google, BlockBeacon, NI R&D

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