strtok function thread safety

后端 未结 2 1278
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 08:25

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 d

相关标签:
2条回答
  • 2020-11-27 08:37

    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.

    0 讨论(0)
  • 2020-11-27 08:54

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

    0 讨论(0)
提交回复
热议问题