libpthread contains around 56 functions implemented from the total of 94 POSIX functions. Out of these some very important and frequently used APIs are pthread_create, pthread_self, pthread_equal, pthread_join, pthread_exit
The pthread_create function will fail if, 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.
Following code shows the simple thread creation:
// The follwing is a GCCE toolchain workaround needed when compiling with GCCE
// and using main() entry point
if(pthread_create(&thread_hdl,NULL,TestFunction, NULL) != 0)
printf("Thread creation failed \n");
printf("Hello Open C!\n");
//Libraries required are libc_r.lib libpthread.lib libthr.lib
The pthread_join function suspends execution of the calling thread until the target thrHandle terminates unless the target thrHandle has already terminated.
int pthread_join (pthread_t thrHandle, void **retValPtr);
The pthread_equal function will return non-zero if the thread IDs t1 and t2 correspond to the same thread, otherwise it will return zero.
int pthread_equal (pthread_t t1, pthread_t t2);
The following code shows the usage and functionality of pthread_equal():
pthread_t thread_1, thread_2;
/* Create Thread-1 */
if(pthread_create(&thread_1, NULL, ThreadFunction, NULL) != 0)
perror("Thread-1 Creation Failed\n");
/* Create Thread-2. */
if(pthread_create(&thread_2, NULL, ThreadFunction, NULL) != 0)
perror("Thread-2 Creation Failed\n");
/* Here,pthread_equal should return a zero value,since they are not equal*/
if(pthread_equal(thread_1, thread_2) != 0)
printf("Both the threads correspond to the same thread\n");
The pthread_exit function cannot return to its caller. The pthread_exit function terminates the calling thread and makes the value retValPtr available to any successful join with the terminating thread
void pthread_exit (void *retValPtr);
Calling pthread_exit() results in exiting the process with an exit status of 0 after the last thread has been terminated.
pthread_detach is used to detach a thread.
libc_r.lib libpthread.lib libthr.lib
int pthread_detach (pthread_t thrHandle);
If successful, the pthread_detach function will return zero. Otherwise an error number will be returned to indicate the error.
The pthread_detach function is used to indicate to the implementation that storage for the thread thrHandle can be reclaimed when the thread terminates. If thrHandle has not terminated, pthread_detach will not cause it to terminate. The effect of multiple pthread_detach calls on the same target thread is unspecified.
Please do add the example code for other APIs in libpthread if you can.