POSIX stands for Portable Operating System Interface, the threads created by this interface are portable on different platforms, as opposed to the threads created by calling system API clone() as explained in the previous post.
This interface has been specified by the IEEE POSIX 1003.1c standard.
POSIX threads library is known as libpthread library. This library provides certain basic routines:
1. pthread_create
2. pthread_join
3. pthread_detach
4. pthread_self
5. pthread_equal
6. pthread_kill
7. pthread_exit
8. pthread_attr_init
When a multi-threaded program starts executing, already one thread exists which runs the main() function. To create a new thread, program uses the pthread_create() API, as explained in the example below:
-------------------------------HELLO.c-------------------------------------------------------
----------------------------------------------------------------------------------------------#include
/* standard I/O routines */
#include/* pt
hread functions and data structures */
/* function to be executed by the new thread */
void* PrintHello(void* data)
{
int my_data = (int)data; /* data received by thread */
printf("Hello from new thread - got %d\n", my_data);
pthread_exit(NULL); /* terminate the thread */
}
/* like any C program, program's execution begins in main */
int main(int argc, char* argv[])
{
int rc; /* return value */
pthread_t thread_id; /* thread's ID (just an integer) */
int t = 11; /* data passed to the new thread */
/* create a new thread that will execute 'PrintHello' */
rc = pthread_create(&thread_id, NULL, PrintHello, (void*)t);
if(rc) /* could not create thread */
{
printf("\n ERROR: return
code from pthread_create is %d \n", rc);
exit(1);
}
printf("\n Created new thread (%d) ... \n", thread_id);
pthread_exit(NULL); /* terminate the thread */
}
1. Inside main, declare a variable of type pthread_t.
2. Call pthread_create() API to bring life to a new thread.
3. pthread_create() takes 4 arguments:
1. Thread id.
2. Attributes for the new thread, If NULL pointer is passed, it sets the default values as attributes.
3. 3rd argument is the function name to be executed by the new thread.
4. 4th argument holds the arguments required by the function passed as argument 3.
4. After pthread_create successfully returns, the program has 2 running threads.
5. The call to pthread_exit causes the current thread to exit and free any thread-specific resources.
In previous posts, we understood the unpredictable timings at which threads are invoked.
pthread_join() API ensures that all the threads are executed before the ma
in thread dies.
But still the uncertainty prevails regarding the execution order of all the threads created by the main thread.
Mutex, condition variables and semaphores come handy for synchronization of threads which can further prevent deadlocks.
MuTex: ( Mutual Exclusion )
Think of mutex as a key for a lock.
Even if there are 2 running threads, we need a mutex, so that the other thread does not get access to a certain section of code, until the first thread has finished its task.
Question - Answer Round:
1. Think of scenarios where mutex will also fail
2. Think os scenarios where mutex is mandatory
3. Does mutex make code more efficient to use?
4 comments:
Good to see the techie bee posting again.
Just to addon: POSIX library has led to the development of multi-threading concepts in the managed runtime space. But with multi-core processors coming up, usage of threads is increasing by leaps and bounds. Harnessing that power via the OS is something we still don't see much, except in the new Apple Snow Leopard's Grand Central Dispatch (which Apple has now made open-source).
I believe google as it has a wider search database, but others may differ.
I was looking for this wonderful forum, and found it. Now I'm your regular user. rnThanks for the administration of this forum.
Guy .. Beautiful .. Wonderful .. I will bookmark your blog and take the feeds alsoI am happy to search out numerous useful info here within the post, we want work out more techniques in this regard, thanks for sharing. . . . . .
Post a Comment