0% found this document useful (0 votes)
38 views

Help For Assignment 8

This document discusses implementing POSIX threads in C programs. It describes the pthread_create and pthread_join functions used to create and wait for POSIX threads. It also provides a sample code that creates threads to run a function and joins the threads.

Uploaded by

Mera Pura Naam
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)
38 views

Help For Assignment 8

This document discusses implementing POSIX threads in C programs. It describes the pthread_create and pthread_join functions used to create and wait for POSIX threads. It also provides a sample code that creates threads to run a function and joins the threads.

Uploaded by

Mera Pura Naam
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/ 2

Assignment 8: POSIX Threads

Implementing POSIX threads in a C program:

C function used to create POSIX threads:


#include <pthread.h>
void* start_routine(void *);
int pthread_create(pthread_t *thread, const pthread_attr_t
*attr, void *(*start_routine) (void *), void *arg);

C function used to wait for POSIX threads to end and rejoin parent
process:
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);

Sample code:
void* pth_body(void *args) {
// here give the code for leap year check
printf("Thread number: %d\n", *((int *)args));
}
int main() {
pthread_t thread; // an id for a pthread
int count = 0;
while(1) {
count++;
// create a thread with pth_body as the function it
executes
pthread_create(&thread, NULL, &pth_body, &count);
//wait for the thread to terminate
pthread_join(thread, NULL);
sleep(1); // to slow down the outputs
}
}
Output:

You might also like