Open System Services Programmer's Guide

Example 74 Using the Reentrant Function strtok_r
/* reent.c - Using the reentrant function strtok_r
*
* Compilation steps:
*
* /usr/bin/c89 -Wextensions -Wsystype=oss -c reent.c -o reent.o
* /usr/bin/nld /usr/lib/crtlmain.o reent.o ./libspt.a -obey \
* /usr/lib/libc.obey -lzcplsrl -o reent
*/
#include<spthread.h>
#include<stdio.h>
void *gettok(void *args)
{
char str[25] = "a,b,c,";
char str5[25] = "x,y,z,";
char **lasts;
char *str1 = NULL;
char *ptr;
lasts = &str1;
ptr = strtok_r(str,",",lasts);
while(ptr != NULL)
{
printf("\n thread - %d token - %s",args,ptr);
sleep(2);
ptr = strtok_r(NULL,",",lasts);
}
ptr = strtok_r(str5,",",lasts);
while(ptr != NULL)
{
printf("\n thread - %d token - %s",args,ptr);
sleep(2);
ptr = strtok_r(NULL,",",lasts);
}
printf("\n");
}
void main()
{
pthread_t T[2];
int ret_value,t;
for(t=0;t < 2;t++)
{
ret_value = pthread_create(&T[t],NULL,(void *)gettok,(void *)t);
if (ret_value != 0)
{
printf("Error in creating the thread");
exit(1);
}
}
pthread_join(T[0],NULL);
pthread_join(T[1],NULL);
exit(0);
}
Thread-Aware Toolkit Functions and Types
Functions written using the thread-aware toolkit (Table 61) allow one or more threads to perform
work on one or more files without any one thread unnecessarily blocking another thread. To enable
these functions, you must include the spthread.h header file. You can use these functions with
either the spt_*() functions or the spt_*x() functions.
368 Using the Standard POSIX Threads Library