Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.


From Wiki
Jump to: navigation, search
Article Metadata
Created: spark55 (13 Sep 2007)
Last edited: hamishwillee (27 Jul 2012)



libpthread is an Open C library which is an implementation of POSIX(IEEE Std1003.1c standard interface).

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:

#include <stdio.h>
#include <pthread.h>
// The follwing is a GCCE toolchain workaround needed when compiling with GCCE
// and using main() entry point
#ifdef __GCCE__
#include <staticlibinit_gcce.h>
void *TestFunction(void*)
printf("Test function....\n");
return NULL;
int CreateThread()
pthread_t thread_hdl;
if(pthread_create(&thread_hdl,NULL,TestFunction, NULL) != 0)
printf("Thread creation failed \n");
return -1;
int main(void)
printf("Hello Open C!\n");
return 0;
//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);

pthread _equal

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():

void *ThreadFunction(void*)
return NULL;
void MainFunction()
pthread_t thread_1, thread_2;
/* Create Thread-1 */
if(pthread_create(&thread_1, NULL, ThreadFunction, NULL) != 0)
perror("Thread-1 Creation Failed\n");
return -1;
/* Create Thread-2. */
if(pthread_create(&thread_2, NULL, ThreadFunction, NULL) != 0)
perror("Thread-2 Creation Failed\n");
return -1;
/* 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");
return -1;


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 
#include <pthread.h>
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.

This page was last modified on 27 July 2012, at 04:19.
96 page views in the last 30 days.