First, I am noob at threads. I am modifying a program that runs on a windows server platform to use two parallel threads and mutex locks on global resources. I compile with cygwin's gcc compiler
gcc source.c -o out.exe -lcurl (the program also uses curl)
The error it gives me:
/cygdrive/c/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccr8d7lT.o:myevault.2.1.0.c.text+0
x9e03): undefined reference to `_p_mutex_lock'
collect2: ld returned 1 exit status
NOTE: adding -lpthread to the gcc command does no good
Here are the (I think) relevant pthread and mutex sections from the program:
=============================================
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <winbase.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <curl/curl.h>
#include <pthread.h>
#include <unistd.h>
/* also in the global definitions */
pthread_mutex_t CheckLogMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t set_state_and_ftpMutex = PTHREAD_MUTEX_INITIALIZER;
//function one
static void *one(void *vptr_args)
{
// function code
//.
//.
//.
pthread_mutex_lock( &set_state_and_ftpMutex );
//global resource code
pthread_mutex_unlock( &set_state_and_ftpMutex );
return NULL;
}
//function two
static void *two(void *vptr_args)
{
// function code
//.
//.
//.
pthread_mutex_lock( &set_state_and_ftpMutex );
//global resource code
pthread_mutex_unlock( &set_state_and_ftpMutex );
return NULL;
}
main
{
pthread_t thread_do_one;
pthread_t thread_do_two;
//code
//...
if (pthread_create(&thread_do_one, NULL, one, NULL) != 0)
{
//handle thread creation error
}
if (pthread_create(&thread_do_two, NULL, two, NULL) != 0)
{
//handle thread creation error
}
pthread_join(one, NULL);
//code...
}
================================================== ===
So I figure I am missing a some library file or something, BUT I wanted to see if a simple example would work. From a tutorial on threads, I created a simple "threads with mutex" test program. It compiles and runs fine. (compiled with gcc source.c -o out.exe)
/* this program compiles and runs fine */
Code:
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
int count = 0;
void* function1( void* arg )
{
int tmp = 0;
while( 1 ) {
pthread_mutex_lock( &mymutex );
tmp = count++;
pthread_mutex_unlock( &mymutex );
printf( "Count is %d\n", tmp );
/* snooze for 1 second */
sleep( 1 );
}
return 0;
}
void* function2( void* arg )
{
int tmp = 0;
while( 1 ) {
pthread_mutex_lock( &mymutex );
tmp = count--;
pthread_mutex_unlock( &mymutex );
printf( "** Count is %d\n", tmp );
/* snooze for 2 seconds */
sleep( 2 );
}
return 0;
}
int main( void )
{
pthread_t one;
pthread_t two;
pthread_create( &one, NULL, &function1, NULL );
pthread_create( &two, NULL, &function2, NULL );
/* Let the threads run for 60 seconds. */
sleep( 60 );
return 0;
}
At this point, I don't have any ideas. Any help will be MUCH appreciated.
Leon