My main()'s job is to read from a file and innitiate a bunch of threads. Those threads than use the take() method to get a single line from a file. Eventually my main() is done processing file than triggers the boolean value quit to be true. The thread's should all shut down then...Also, I have no idea if i'm using strtok_r() right, I tried a few different ways. Thanks, feel free to ask any questions about the code.
Code:
void *thread( void *dummy )
{
// the consumer thread's local histogram array
ulong hist[ HISTOGRAM_SIZE ];
char *saveptr; // for strtok_r (not sure how to use properly)
char *token; //words in a line
char *item;
int length;
int i=0;
// initialize the array to all zeroes
for(i = 0; i < HISTOGRAM_SIZE; i++)
hist[i] = 0;
// take and process items till shutdown
item = take(); //get a line from a file
while( 1 && !quit )//check it's not time to quit
{
while( (token = strtok_r( item, DELIMS, &saveptr )) != NULL ) //get all the tokens in the line
{
//token = strtok_r( item, DELIMS, &saveptr );
item = saveptr;
printf( "%s", token );
length = strlen( token ); //length of the word
if( length < 10 )
hist[ length - 1 ] += 1; //category 1 to 9
else
hist[ HISTOGRAM_SIZE - 1 ] += 1; //category 10+
}
item = take(); //get next item
}// while
//Use mutex to lock, were adding a local array's values to a global array's values
pthread_mutex_lock( &histogram_lock );
//cycle local array totals adding to global
//array totals
for( i=0; i < HISTOGRAM_SIZE; i++ )
histogram[ i ] += hist[ i ];
pthread_mutex_unlock( &histogram_lock ); //unlock
pthread_exit(NULL);
} // end consumer_thread