Open System Services Programmer's Guide

Example 94 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 -lzcredll -o reent
*/
#define _PUT_MODEL_
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include myio.h /* contains my_printf which is process blocking */
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)
{
my_printf("\n thread - %d token - %s",args,ptr);
sleep(2);
ptr = strtok_r(NULL,",",lasts);
}
ptr = strtok_r(str5,",",lasts);
while(ptr != NULL)
{
my_printf("\n thread - %d token - %s",args,ptr);
sleep(2);
ptr = strtok_r(NULL,",",lasts);
}
my_printf("\n");
}
int main()
{
pthread_t T[2];
int ret_value,t;
for(t=0;t < 2;t++)
{
ret_value = pthread_create(&T[t],NULL,gettok,(void *)t);
if (ret_value != 0)
{
my_printf("Error in creating the thread");
exit(1);
}
}
pthread_join(T[0],NULL);
pthread_join(T[1],NULL);
exit(0);
}
410 Using the POSIX User Thread (PUT) Model Library