4.6 Creating and Operating POSIX Threads
4.6 Creating and Operating POSIX Threads
Session 4.6
Shripad V Deshpande
4
POSIX Thread (PThread)
• 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.
• Pthreads are best used with programs that can
be organized into discrete, independent tasks
which can execute concurrently.
5
Pthread Management
• The main() function comprises a single,
default thread.
• pthread_create() creates a new thread and
makes it executable.
• The maximum number of threads that may be
created by a process in implementation
dependent.
• Once created, threads are peers, and may
create other threads.
Creating Threads
• pthread_create() creates a new thread and makes it
executable.
int pthread_create( pthread_t *thread, pthread_attr_t *attr, void
*(*thread_function)(void *), void *arg );
• 1st arg – pointer to the identifier of the created
thread
• 2nd arg – thread attributes. If null, then the
thread is created with default attributes
• 3rd arg – pointer to the function the thread will
execute
• 4th arg – the arguments of the executed function
• returns 0 for success
Waiting threads
int pthread_join( pthread_t thread, void **thread_return )
• This function is called in main thread so it
will wait for daughter thread to finish
• 1st arg – the thread to wait for
• 2nd arg – pointer to a pointer to the return
value from the thread
• returns 0 for success
• threads should always be joined; otherwise, a
thread might keep on running even when the
main thread has already terminated
Terminating Threads
• Several ways to terminate a thread:
• The thread is complete and returns
• The pthread_exit() method is called
• The pthread_cancel() method is invoked
• The exit() method is called
• The pthread_exit() routine is called after a
thread has completed its work and is no longer
required to exist.
• If the main program finishes before the thread(s)
do, the other threads will continue to execute if
a pthread_exit() method exists.
• The pthread_exit() method does not close files;
any files opened inside the thread will remain
open, so cleanup must be kept in mind.
Pthread Example
int main (int argc, char *argv[])
#include <pthread.h>
{
#include <stdio.h>
pthread_t threads[NUM_THREADS];
#include <stdlib.h>
int rc, t;
for(t=0; t<NUM_THREADS; t++)
#define NUM_THREADS 5
{
printf("In main: creating thread
void *PrintHello(void *threadid)
%d\n", t);
{
rc = pthread_create(&threads[t],
int tid;
NULL, PrintHello, (void *)t);
tid = (int)threadid;
if (rc)
printf("Hello World! It's me,
{
thread #%d!\n", tid);
printf("ERROR; return code from
pthread_exit(NULL);
pthread_create() is %d\n", rc);
}
exit(-1);
}
}
pthread_exit(NULL);
}
Pthread Example - Output
In main: creating thread 0
In main: creating thread 1
Hello World! It's me, thread #0!
In main: creating thread 2
Hello World! It's me, thread #1!
Hello World! It's me, thread #2!
In main: creating thread 3
In main: creating thread 4
Hello World! It's me, thread #3!
Hello World! It's me, thread #4!
Next Session 4.7 ->
Memory Management, Virtual Memory