How to achieve Thread Synchronization in posix thread ( pthread )
Filed under: Linux Programming Interview Questions, Mutithreading Interview Questions
Synchronization in the multithreaded application on posix thread can be achieved through mutex. Synchronization is required when all the thread are trying to access the shared data and to corruption of the shared data mutual exclusion is required.
Below are steps to perform mutual exclusion on shared data or to achieve thread synchronization
1. Declare an object of type pthread mutex t .
2. Initialize the object by calling pthread mutex init(). PTHREAD_MUTEX_INITIALIZER can also be used for initializing the thread
3. Call pthread mutex lock() to gain exclusive access to the shared data
object.
4. Call pthread_mutex_unlock() to release the exclusive access and allow another thread to use the shared data object.
5. Get rid of the object by calling pthread_mutex_destroy() at the end of the program.
Program:
#include
pthread_mutex_t lock ;
int shared data ;
// Often shared data is more complex than just an int.
void * threadfunction ( void * arg )
{
int i ;
for ( i = 0 ; i < 1024*1024; ++i ) {
// Access the shared data here.
pthread_mutex_lock(&lock ) ;
shar ed data++;
pthread_mutex_unlock(&lock ) ;
}
return NULL;
}
int main ( void)
{
pthread_t thread ID ;
void * exitstatus ;
int i ;
// Initialize the mutex before trying to use it.
pthread_mutex_init (&lock , NULL) ;
pthread_create (&thread ID , NULL, threadfunction , NULL) ;
// Try to use the shared data.
for ( i = 0 ; i < 1 0 ; ++i )
{
sleep (1) ;
pthread_mutex_lock(&lock ) ;
p r i n t f ( “\rShared integer ‘s value = %d\n” , shared data ) ;
pthread mutex_unlock(&lock ) ;
}
p r i n t f ( “\n” ) ;
pthread_join ( thread ID , &e x i t s t a t u s ) ;
// Clean up the mutex when we are finished with it.
pthread_mutex_destroy(&l o ck ) ;
}
How to avoid memory leakage in multithreaded application
Filed under: Linux Programming Interview Questions, Mutithreading Interview Questions
The parent thread should always free the memory which has been created by the child thread otherwise memory leakage issue occurs .
For example
char * t h r e a d r e s u l t ;
t h r e a d r e s u l t = ( char *) e x i t s t a t u s ;
p r i n t f ( “I got %s back from the thread .\n” , t h r e a d r e s u l t ) ;
f r e e ( e x i t s t a t u s ) ;
Here exitstatus is the pointer to the memory which has been allocated by the child thread and parent thread free those memory after the termination of the child thread
What is dangling pointer situation in Multithreaded application and how to avoid it
Filed under: Linux Programming Interview Questions, Mutithreading Interview Questions
void * t hr e ad f un c t i o n ( void *)
{
char b u f f e r [ 6 4 ] ;
// Fill up the buffer with something good.
return b u f f e r ;
}
In the above code we are trying to return an automatic variable which get out of scope once the function return and the pointer would point to invalid memory location.
To avoid this dangling pointer problem data must be allocate with malloc so that memory get created dynamically and not locally in the function.
void * t hr e ad f un c t i o n ( void *)
{
char * b u f f e r = ( char *) mal loc ( 6 4 ) ;
// Fill up the buffer with something good.
return b u f f e r ;
}
How to create thread, wait thread and cancel thread in pthread (posix thread)
Filed under: Linux Programming Interview Questions, Mutithreading Interview Questions
Posix thread can be created with the pthread_create() functions . It takes 4 arguments
First argument is the thread ID. The second parameter is a pointer to a thread attribute object that you can use to set the thread’s attributes. The null pointer means to use default attributes. The third parameter is a pointer to the function the thread is
to execute. The final parameter is the argument to the function. By using void pointers here, any sort of data could potentially be sent to the thread function provided proper casts are applied.
pthread_join() function wait for the termination of the thread and it return exit status of the thread. The thread function take void* as the argument and return a void * data type so that any data can be send and received from the thread function.
pthread_cancel() cancel the thread but it should be avoided as it can create a situation of memory leakage if memory is not freed which were allocated to the thread.
Below is the simple program to create and join the thread
#include <pthread . h>
/*
* The function to be executed by the thread should take a
* void* parameter and return a void* exit status code.
*/
void * t hr e ad f un c t i o n ( void * arg )
{
// Cast the parameter into what is needed.
int* incoming = ( int *) arg ;
// Do whatever is necessary using *incoming as the argument.
// The thread terminates when this function returns.
return NULL;
}
int main ( void)
{
pthr e ad t thread ID ;
void* e x i t s t a t u s ;
int value ;
// Put something meaningful into value.
value = 4 2 ;
// Create the thread , passing &value for the argument.
pthread_create (&thread ID , NULL, thr e ad func t i on , &value ) ;
// The main program continues while the thread executes.
// Wait for the thread to terminate.
p t h r e a d j o i n ( thread ID , &e x i t s t a t u s ) ;
// Only the main thread is running now.
return 0 ;
}
