the posted code contains several problems, like not waiting for the thread to exit.
Suggest the following code, which has been tested and works correctly on ubuntu linux 14.04
Notice the use of pthread_mutex_lock() and pthread_mutex_unlock()
Notice the use of pthread_join()
In general, code needs to be written as simply as possible, but no simpler.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_JOBS (25)
#define MAX_DATA_LEN (25)
struct node
{
int last;
char data[MAX_DATA_LEN];
struct node *next;
};
static pthread_t thread_id;
static struct node *jobsList = NULL;
static pthread_mutex_t jobsMutex = PTHREAD_MUTEX_INITIALIZER;
void putNode( struct node * newNode );
struct node *getNode( void );
struct node *createNode( void );
void* process_job( void* );
int main( void )
{
if( 0 != pthread_create( &thread_id, NULL, process_job, NULL) )
{
perror( "pthread_create failed" );
exit( EXIT_FAILURE );
}
// implied else, pthread_create successful
int done = 0;
while( !done )
{
struct node *newNode = NULL;
if( NULL == (newNode = createNode() ) )
{
done = 1;
}
else
{
putNode( newNode );
}
}
pthread_join( thread_id, NULL);
pthread_mutex_destroy( &jobsMutex );
return 0;
} // end function: main
struct node* createNode()
{
static char dataCount = 0;
char data[ MAX_DATA_LEN ] = {'\0'};
struct node *newNode = NULL;
if( MAX_JOBS > dataCount )
{
if( NULL != (newNode = malloc( sizeof( struct node ) ) ) )
{
newNode->last = ( dataCount < (MAX_JOBS-1) )? 0 : 1;
dataCount++;
newNode->next = NULL;
sprintf( data, "%s %d", "this is node: ", dataCount );
strcpy( newNode->data, data );
}
}
return newNode;
} // end function: createNode
void putNode( struct node *newNode )
{
struct node *current = NULL;
pthread_mutex_lock( &jobsMutex );
if( !jobsList)
{
jobsList = newNode;
}
else
{
for( current = jobsList; current->next; current = current->next);
current->next = newNode;
}
pthread_mutex_unlock( &jobsMutex );
} // end function: putNode
// the thread process
void *process_job( void *parm )
{
(void) parm;
struct node * newNode;
int done = 0;
while( !done )
{
while(1)
{
if(NULL == (newNode = getNode() ) )
{
sleep(1);
}
else
{
break;
}
}
printf( "newNode data: %s\n", newNode->data);
free( newNode );
if( newNode->last ) done = 1;
}
pthread_exit( 0 );
} // end function: process_job
struct node *getNode()
{
pthread_mutex_lock( &jobsMutex );
if( !jobsList )
{
pthread_mutex_unlock( &jobsMutex );
return NULL;
}
struct node *current = NULL;
current = jobsList;
jobsList = jobsList->next;
pthread_mutex_unlock( &jobsMutex );
return current;
} // end function: getNode
The above code results in the following output:
newNode data: this is node: 1
newNode data: this is node: 2
newNode data: this is node: 3
newNode data: this is node: 4
newNode data: this is node: 5
newNode data: this is node: 6
newNode data: this is node: 7
newNode data: this is node: 8
newNode data: this is node: 9
newNode data: this is node: 10
newNode data: this is node: 11
newNode data: this is node: 12
newNode data: this is node: 13
newNode data: this is node: 14
newNode data: this is node: 15
newNode data: this is node: 16
newNode data: this is node: 17
newNode data: this is node: 18
newNode data: this is node: 19
newNode data: this is node: 20
newNode data: this is node: 21
newNode data: this is node: 22
newNode data: this is node: 23
newNode data: this is node: 24
newNode data: this is node: 25
You can easily modify the code for your specific struct definition, method of generating struct instances, method of processing the struct when received in the thread.
Listimplementation are you using? - John Zwinck