Wednesday, 5 February 2014

Multithreading in C++

 

The thread is the process of executing the set of instructions independent from each other by sharing the resources.
Then process is also a set of instructions, which produce certain output.
What makes the difference between the thread and process? Here are few…
Thread
Process
Thread share the same address space
Process will have different address space
Context switching is slow here.
Context switching is faster between process
Thread depend upon the process.
Process is typically independent

In above difference context switching is something storing and retrieving the states of the threads.

Multithreading:
Multithreading is the parallel execution of the programs by sharing the resources like memory.

Multithreading in C++:
C++ doesn’t have an built-in program to handle the threads. We need to use the API to handle the threads. POSIX(Portable operating system interface) threads provides an API to create and manipulate threads. POSIX threads usually called as an pthreads.
POSIX is an family of IEEE standard which provides the lots of API’s.
All the implementation of the POSIX thread are under the pthread.h
All functions of POSIX threads begin with a prefix “pthread_”.
Creating a thread:

PROTOTYPE:
pthread_create(pthread_t *thread_info, const pthread_attr_t *attribute, function_name, (void *)myfunction_args)
EXAMPLE:
pthread_t mythread;
pthread_attr_t thread_attr;
pthread_create(&mythread, &thread_attr, my_function, &i)

argument
Description
Need for this argument
pthread_t *thread_info
Unique identifier for the thread. Contains the information about the thread you were creating. It is of Pointer type. Pass the variable of struct pthread_t.
You will have an multiple threads in your program, to identify and manipulate a single thread this argument becomes useful.
const pthread_attr_t *attribute
Attributes to the thread are set here. Attributes in thread like priority, etc
Among several threads if you need to set priority to the particular thread, you can configure with this pointer
myfunction
This method is called after the successful creation of thread. The code that should be executed under this thread can be placed inside that function. If you need to specify the argument. Don’t do it here. The next parameter is used for passing a argument for the function “myfunction”
The code that is need to execute under the created thread can be placed inside the “myfunction”.

(void *)myfunction_args
The arguments for the myfunction is given here. It is of void pointer type.  The reason why given as void pointer is, there is no possibility that programmer will pass a int or float to the function, so in general it is passed as a void pointer. We can convert to any datatype further.
This argument is needed because if programmer intend to pass argument for a myfunction then he can use this argument to do so. If programmer wishes no argument is needed then he can pass NULL.

passing paramter as null:
You can configure the parameter of the pthread_create as NULL which indicates that the default values should be taken.
For instance,
pthread_create(&mythread, NULL, my_function, NULL)
In above statement, mythread will be created by taking the default attributes. Because the the second parameter is given as NULL. If you give the fourth parameter as NULL. Then the void pointer variable will be assigned as NULL.
This not means the creating the myfuntion with no arguments. Don’t confuse with that. It will just assign the NULL value to the void pointer.
Then your thinking will be what about configuring the 1st and 3rd parameter as NULL. If you pass them as NULL then your code will become useless, this is because if create thread without thread function, then where the code for that thread created.

synchronization in threads:


THREAD POOL:
Thread pool is the series of threads which is usually placed in the queue for performing some tasks. The input and output to the thread pool might be queue. Thread in thread pool will process the input queue one by one and places the result in result queue.
Threads are not destroyed after the completion of its tasks. After completion of task thread will take a new task or it will goes to sleep state.

The following scenario may occur depend upon the needs,
1.      Thread will terminate or sleep when allocated task in completed

Thread pool is much useful because.
1.      It reduces the overhead of creating and destroying threads.

2.      Thread creation will take much time. So It reduce the wait time for the client.