News for hackers. Stuff that matters.
Re:String tokenization in C by pasokan on Saturday December 15, 2018 @06:34AM (#18688021)
It used to be that gcc will warn against strtok and recommend strsep instead. Do not know what the status is today
Re:String tokenization in C by tinus_hn on Saturday December 15, 2018 @06:45AM (#18688047)
Strtok is not thread safe and can’t be made thread safe without changing the API. You should not use it.
Re:String tokenization in C by morbusfonticuli on Saturday December 15, 2018 @06:53AM (#18688060)
> Strtok is not thread safe and can’t be made thread safe without changing the API. You should not use it.

Well, there is already a thread-safe variant [0]: > The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.

[0] https://linux.die.net/man/3/strtok_r

Re:String tokenization in C by seba_dos1 on Saturday December 15, 2018 @07:30AM (#18688140)
...with different API :P
Re:String tokenization in C by enriquto on Saturday December 15, 2018 @09:03AM (#18688414)
of course! the original API cannot be made re-entrant
Re:String tokenization in C by heinrichhartman on Saturday December 15, 2018 @07:42AM (#18688158)
Well, strtok could use thread local variables to store intermediate state, to make it threadsafe while maintaining the same API. Not saying this is a good idea, but technically it would work, no?
Re:String tokenization in C by nly on Saturday December 15, 2018 @08:56AM (#18688388)
You could, but that would change the behaviour of existing programs. It might well be that there are well-defined programs out there that use strtok across separate, but properly synchronized, threads.

This is why it's crucial to get APIs right first time.

Re:String tokenization in C by Sharlin on Saturday December 15, 2018 @07:53AM (#18688188)
Yes, as long as you can guarantee that there’s only one tokenization going on per thread at a time.
Re:String tokenization in C by zakk on Saturday December 15, 2018 @08:12AM (#18688257)
That’s not a good reason not to use it.

A function can be not thread-safe and still safe to use in single-threaded programs.

The point is that strtok is not a good choice even for single-threaded code.

Re:String tokenization in C by johnisgood on Saturday December 15, 2018 @08:31AM (#18688309)
> The point is that strtok is not a good choice even for single-threaded code.

Why isn't it a good choice exactly? Could you sum it up?

Re:String tokenization in C by enriquto on Saturday December 15, 2018 @09:04AM (#18688417)
Maybe he means that the function is not re-entrant. You cannot run a loop that tokenizes a string, and somewhere inside, you call a function that uses strtok itself. This can happen inadvertently.