CodexBloom - Programming Q&A Platform

advanced patterns with `strtok` and Thread Safety in C - Need Insights

๐Ÿ‘€ Views: 0 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-16
threads strtok c C

I'm working on a personal project and I'm working with unexpected behavior when using `strtok` in a multi-threaded application... My code runs perfectly fine when executed in a single thread, but when I introduce threads, the results become inconsistent. Here's a simplified version of my code: ```c #include <stdio.h> #include <string.h> #include <pthread.h> #define NUM_THREADS 2 void* threadFunc(void* arg) { char* str = (char*)arg; char* token = strtok(str, " "); while (token != NULL) { printf("Thread %ld: %s\n", pthread_self(), token); token = strtok(NULL, " "); } return NULL; } int main() { char str1[] = "Hello world from thread 1"; char str2[] = "Hello world from thread 2"; pthread_t threads[NUM_THREADS]; pthread_create(&threads[0], NULL, threadFunc, str1); pthread_create(&threads[1], NULL, threadFunc, str2); for (int i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } return 0; } ``` When I run this, the output is sometimes completely garbled, with tokens from different strings interleaved. I understand that `strtok` is not thread-safe because it uses a static buffer to store the token state. To work around this, I tried using `strtok_r`, but I still get mixed tokens. Hereโ€™s the modified thread function: ```c void* threadFunc_r(void* arg) { char* str = (char*)arg; char* saveptr; char* token = strtok_r(str, " ", &saveptr); while (token != NULL) { printf("Thread %ld: %s\n", pthread_self(), token); token = strtok_r(NULL, " ", &saveptr); } return NULL; } ``` This still doesnโ€™t solve the scenario. Am I passing the string incorrectly to the threads, or is there another question with my implementation? Any insights into how to handle string tokenization safely in a multi-threaded C application would be greatly appreciated. Thanks in advance! I'm working on a API that needs to handle this. My development environment is CentOS. Is this even possible?