#include #include #include #include #include #include #include // my_strtok library ---------------------------------------------------------- int my_actual = 0; // pthread_mutex_t my_mutex; char* strtokc(char* text, char delim) { // pthread_mutex_lock(&my_mutex); static char* current = NULL; if (text) { current = text; } else if (current == NULL) { return NULL; } else { ++current; } char* start = current; while (*current && *current != delim) { ++current; } if (*current == delim) { *current = '\0'; } else { current = NULL; } return start; // pthread_mutex_unlock(&my_mutex); } // tester program ------------------------------------------------------------- char* texts[] = { "a,b,cd,efg", "no commas", "01,02,,04,05,10", }; char* results[3][10] = {NULL}; size_t thread_count = 1; void* split(void* data); void signal_handler(int signal_id); int main() { //signal(SIGINT, signal_handler); #ifdef SERIAL char buffer[1919]; snprintf(buffer, 1919, ",,b"); for (char* token = strtokc(buffer, ','); token; token = strtokc(NULL, ',')) { printf("[%s]\n", token); } #else // pthread_mutex_init(&my_mutex, NULL); thread_count = 3; pthread_t threads[thread_count]; for (size_t index = 0; index < thread_count; ++index) { pthread_create(&threads[index], NULL, split, (void*)index); } for (size_t index = 0; index < thread_count; ++index) { pthread_join(threads[index], NULL); } for (int index = 0; index < count; ++index) { printf("%d\n", texts[index]); } // pthread_mutex_destroy(&my_mutex); #endif return 0; } /* void* split(void* data) { size_t rank = (size_t)data; for (char* token = strtokc(texts[rank], ','); token; token = strtokc(NULL, ',')) { printf("[%s]", token); } return NULL; } void signal_handler(int signal_id) { fprintf(stderr, "signal_hadler(%d): strtokc(texts[1])=[%s]\n" , signal_id, strtokc(texts[1], ',')); } */