pthread_create returns the value 251 without creating the thread. Does anyone know what the problem is? Please help. The machine is a HP-UX.
I'm new to multi-threading.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will
* execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
/* Wait till threads are complete before
* main continues. Unless we */
/* wait we run the risk of executing an
* exit which will terminate */
/* the process and all threads before the
* threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
-
Edit : On HP-UX11. pthread_create is failing with error 251: Function is not available.
Check whether -lc comes before -lpthread in your link order. If this is the case, then the call would resolve to stub in C-library and could cause this error.
Did you link with -lpthread?
You should use errno.h to see what error 251 is on your system or this should give you a more detailled message :
printf("%s\n", strerror(errno));
Moreover, when using pthread, you should check for the return value of almost every call to pthread* (see the man of every function to check for possible error returned)
For pthread_create, you have at least 2 possible errors (depending on your system and pthread implementation) :
pthread_create() will fail if:
[EAGAIN] The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process [PTHREAD_THREADS_MAX] would be exceeded.
[EINVAL] The value specified by attr is invalid.
-
This compiles and runs on my Linux box with the following result:
Thread 1 Thread 2 Thread 1 returns: 0 Thread 2 returns: 0
So it looks like the problem is not in your code, but something in the environment. I haven't used HP-UX for over 10 years, so I can't help you there.
Shree : Yes, looks like it's got something to do with the environment.claferri : seems it could be the link order, check my edited answer.
0 comments:
Post a Comment