Threads,Like processes are a mechanism to allow a program to do more than one thing at a time.As with processes,threads appear to run concurrently;the linux kernel schedules them asynchronously,interrupting each thread from time to time to give others a chance to execute.We can control the threads using various mechanism which we will discuss later.
Conceptually,a thread exists within a process.When we invoke a program,Linux creates a new process and in that process creates a single thread,which runs the program sequentially.That thread can create additional threads ; all these threads run the same program in the same process,but each thread may be executing a different part of the program at any given time.We can say sequence of execution is a thread.
When a program creates another thread,the creating and the created thread share the same memory space,file descriptors except a local stack is created for individual threads. If one thread changes the value of a variable ,for instance,the other thread subsequently will see the modified value.If one thread closes file descriptor the other threads may not be able to write or read to that file descriptor.If any thread inside a process calls one of the exec functions,all the other threads are ended(the new program may,of course create new threads).
The pthread_create() function creates a new thread.You can refer to man pages about pthread_create().We have to include <pthread.h> header file .A call to pthread_create() returns immediately and the original thread continues executing the instructions following the call.Meanwhile , the new thread begins executing the thread function.Linux schedules both threads asynchronously.
Under normal circumstances, a thread exits in one othe two ways.One way is by returning from the thread function.The return value from the thread function is taken to the return value of the thread.Alternatively,a thread can exit explicitly by calling pthread_exit().This function may be called form within the thread function.The argument to pthread_exit() is the thread’s return value.
We can retrieve the thread exit status using pthread_join().The pthread_join() function shall suspend execution of the calling thread until the target thread terminates, unless the target thread has already terminated. On return from a successful pthread_join(), the value passed to pthread_exit() by the terminating thread shall be made available in the location referenced to pthread_join().When a pthread_join() returns successfully, the target thread has been terminated.
There are signals to control the threads.The pthread_kill() function requests that the signal to be delivered to the specified thread. The signal to be sent is specified by signal number and is either zero or one of the signals from the list of defined signals in the <signal.h> header file. If signal number is zero, error checking is performed, but no signal is sent to the target thread.
It returns 0 on success and a non zero value if pthread_kill() is unsuccessful.That non zero value is to check the error condition.One thing has to be made sure that the signal registration should be done in both the threads i.e the thread which is giving the signal and the target thread which will be killed.
P.S: Refer to man pages related to the arguments and the return values of the functions listed above.