Basic Multithreading using POSIX
threads
Tushar B. Kute,
[Link]
Threads
• A Thread is an independent stream of
instructions that can be schedule to run as such
by the OS.
• Think of a thread as a “procedure” that runs
independently from its main program.
• Multi-threaded programs are where several
procedures are able to be scheduled to run
simultaneously and/or independently by the OS.
• A Thread exists within a process and uses the
process resources.
Threads
• Threads only duplicate the essential
resources it needs to be independently
schedulable.
• A thread will die if the parent process dies.
• A thread is “lightweight” because most of
the overhead has already been
accomplished through the creation of the
process.
The POSIX threads
• POSIX – Portable Operating System
Interface for Unix.
• For UNIX systems, implementations of
threads that adhere to the IEEE POSIX
1003.1c standard are Pthreads.
• Pthreads are C language programming
types defined in the pthread.h
header/include file.
The Pthreads
• The primary motivation behind Pthreads is
improving program performance.
• Can be created with much less OS
overhead.
• Needs fewer system resources to run.
• View comparison of forking processes to
using a pthreads_create subroutine.
Timings reflect 50,000 processes/thread
creations.
Find multithreaded programs in Linux
Single threaded
• ps -aux application
Multithreaded
application
Single threaded program
#include<stdio.h>
int main()
{
while(1)
{
printf("Hello...\n");
}
return 0;
}
Check the entry in process list...
Pthread library
• Pthread Library (60+ functions)
– Thread management: create, exit, detach, join, . .
.
– Thread cancellation
– Mutex locks: init, destroy, lock, unlock, . . .
– Condition variables: init, destroy, wait, timed
wait, . . . . . .
• Programs must include the file pthread.h
• Programs must be linked with the pthread
library (-lpthread)
Pthread: naming convensions
• Types: pthread[_object]_t
• Functions: pthread[_object]_action
• Constants/Macros: PTHREAD_PURPOSE
• Examples:
– pthread_t: the type of a thread
– pthread_create(): creates a thread
– pthread_mutex_t: the type of a mutex lock
– pthread_mutex_lock(): lock a mutex
– PTHREAD_CREATE_DETACHED
pthread_create
• Creates a new thread
• int pthread_create ( pthread_t *thread,
pthread_attr_t *attr, void * (*start_routine) (void
*), void *arg);
• Returns 0 to indicate success, otherwise returns error code...
– thread: output argument for the id of the new thread
– attr: input argument that specifies the attributes of the thread to be
created (NULL = default attributes)
– start_routine: function to use as the start of the new thread must
have prototype: void * foo(void*)
– arg: argument to pass to the new thread routine. If the thread
routine requires multiple arguments, they must be passed bundled
up in an array or a structure
pthread_exit
• Terminates the calling thread
• void pthread_exit(void *retval);
• The return value is made available to
another thread calling a pthread_join()
• The return value of the function serves as
the argument to the (implicitly called)
pthread_exit().
•
pthread_join
• Causes the calling thread to wait for another thread to
terminate
• int pthread_join( pthread_t thread, void **value_ptr);
– thread: input parameter, id of the thread to wait on
– value_ptr: output parameter, value given to
pthread_exit() by the terminating thread (which
happens to always be a void *)
• Returns 0 to indicate success, error code otherwise
• Multiple simultaneous calls for the same thread are not
allowed
Single threaded program
int first() int main()
{ {
int i; int i;
for(i=0; ;i++) first( );
{ for(i=0;;i++)
printf("\nFirst: %d",i); {
sleep(1); printf("\nMain: %d",i);
}
} sleep(1);
}
return 0;
}
Multithreaded program
#include<unistd.h> int main()
#include<stdio.h> {
#include<pthread.h> pthread_t th;
int first() int i;
{ pthread_create(&th, 0,(void
*)&first,NULL);
int i;
for(i=0;;i++)
for(i=0;;i++)
{
{
printf("\nMain: %d",i);
printf("\nFirst: %d",i);
sleep(1);
sleep(1);
}
} pthread_join(th, NULL);
} return 0;
}
Output
Process list
Thank you
This presentation is created using LibreOffice Impress [Link], can be used freely as per GNU General Public License
Blogs
Web Resources
[Link]
[Link]
[Link]
tushar@[Link]