0% found this document useful (0 votes)
2 views3 pages

Multithreading in C Using Posix Threads

The document provides an overview of multithreading in C using the pthreads library, detailing key functions such as pthread_create, pthread_exit, pthread_join, pthread_self, pthread_equal, pthread_cancel, and pthread_detach. Each function is accompanied by its syntax and parameters, explaining how to create, manage, and terminate threads. An example code snippet demonstrates the creation of a thread that prints a message after a brief pause.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Multithreading in C Using Posix Threads

The document provides an overview of multithreading in C using the pthreads library, detailing key functions such as pthread_create, pthread_exit, pthread_join, pthread_self, pthread_equal, pthread_cancel, and pthread_detach. Each function is accompanied by its syntax and parameters, explaining how to create, manage, and terminate threads. An example code snippet demonstrates the creation of a thread that prints a message after a brief pause.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Multithreading in C

The functions defined in the pthreads library include:


a. pthread_create: used to create a new thread
Syntax:
int pthread_create(pthread_t * thread,
const pthread_attr_t * attr,
void * (*start_routine)(void *),
void *arg);
Parameters:
• thread: pointer to an unsigned integer value that returns
the thread id of the thread created.
• attr: pointer to a structure that is used to define thread
attributes like detached state, scheduling policy, stack
address, etc. Set to NULL for default thread attributes.
• start_routine: pointer to a subroutine that is executed by
the thread. The return type and parameter type of the
subroutine must be of type void *. The function has a
single attribute but if multiple values need to be passed
to the function, a struct must be used.
• arg: pointer to void that contains the arguments to the
function defined in the earlier argument
b. pthread_exit: used to terminate a thread
Syntax:
void pthread_exit(void *retval);
Parameters: This method accepts a mandatory
parameter retval which is the pointer to an integer that stores the
return status of the thread terminated. The scope of this variable
must be global so that any thread waiting to join this thread may
read the return status.
c. pthread_join: used to wait for the termination of a thread.
Syntax:
int pthread_join(pthread_t th,
void **thread_return);
Parameter: This method accepts following parameters:
• th: thread id of the thread for which the current thread
waits.
thread_return: pointer to the location where the exit

status of the thread mentioned in th is stored.
d. pthread_self: used to get the thread id of the current thread.
Syntax:
pthread_t pthread_self(void);
e. pthread_equal: compares whether two threads are the same or
not. If the two threads are equal, the function returns a non-zero
value otherwise zero.
Syntax:
int pthread_equal(pthread_t t1,
pthread_t t2);
Parameters: This method accepts following parameters:
• t1: the thread id of the first thread
• t2: the thread id of the second thread
f. pthread_cancel: used to send a cancellation request to a thread
Syntax:
int pthread_cancel(pthread_t thread);
Parameter: This method accepts a mandatory
parameter thread which is the thread id of the thread to which
cancel request is sent.
g. pthread_detach: used to detach a thread. A detached thread does
not require a thread to join on terminating. The resources of the
thread are automatically released after terminating if the thread is
detached.
Syntax:
int pthread_detach(pthread_t thread);
Parameter: This method accepts a mandatory
parameter thread which is the thread id of the thread that must be
detached.

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h> //Header file for sleep(). man 3 sleep for details.

#include <pthread.h>

// A normal C function that is executed as a thread


// when its name is specified in pthread_create()

void *myThread(void *vargp)

sleep(1);

printf("Hello \n");

return NULL;

int main()

pthread_t thread_id;

printf("Before Thread\n");

pthread_create(&thread_id, NULL, myThread, NULL);

pthread_join(thread_id, NULL);

printf("After Thread\n");

exit(0);

You might also like