0% found this document useful (0 votes)
5 views10 pages

Pthread

The document provides an overview of pthread functions, including thread creation and joining. It presents multiple examples demonstrating single and multiple thread creation, passing arguments, and handling return values. The advantages of using multiple threads over a single thread are highlighted through various coding examples.

Uploaded by

jhawardev11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Pthread

The document provides an overview of pthread functions, including thread creation and joining. It presents multiple examples demonstrating single and multiple thread creation, passing arguments, and handling return values. The advantages of using multiple threads over a single thread are highlighted through various coding examples.

Uploaded by

jhawardev11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

pthread

by Tanmoy Maitra
Functions on pthread:
<pthread.h>
• Thread Creation:
int pthread_create(pthread_t *restrict thread, const pthread_attr_t
*restrict attr, typeof(void *(void *)) *start_routine, void *restrict arg);
On success, pthread_create() returns 0; on error, it returns an error
number
• Thread Join:
int pthread_join(pthread_t thread, void **value_ptr);
If successful, the pthread_join() function returns 0; otherwise, an error
number
Example 1: Single Thread Creation without Passing any
Argument
void *example(); Attributes of x1
Store ID of x1
int main() { Passing Arg. by x1
pthread_t x1;
pthread_create(&x1, NULL, example, NULL);
pthread_join(x1, NULL); Return value by example procedure
return 0; }
void *example() { Run
$> gcc -o p1 a.c
printf(“Example of single thread”); $> ./p1
Example of single thread
return NULL; }
Example 2: Single Thread Creation with Passing Argument

void *example(void *y1);


int main() { Type casting is required
pthread_t x1; int value1 = 5;
pthread_create(&x1, NULL, example, (void*) &value1);
pthread_join(x1, NULL);
return 0; }
void *example(void *y1) { Run
int *y2 = (int *) (y1); $> gcc -o p2 b.c
printf(“Single thread with arg.: %d”, *y2); $> ./p2
return NULL; } Single thread with arg.: 5
Example 3: Multiple Threads Creation with Passing
Argument
void *example(void *y1);
int main() {
pthread_t x1, x2;
int value1 = 5; int value2 = 10;
pthread_create(&x1, NULL, example, (void*) &value1);
pthread_create(&x2, NULL, example, (void*) &value2);
pthread_join(x1, NULL); pthread_join(x2, NULL); return 0; }
Run
void *example(void *y1) { $> gcc -o p3 d.c
int *y2 = (int *) (y1); $> ./p3
printf(“Arg.: %d\n”, *y2); Arg.: 5
return NULL; } Arg.: 10
Example 4: Advantage of Multiple Threads over Single
Thread
void *example(void *y1);
int main() {
pthread_t x1, x2;
int value1 = 5; int value2 = 10;
pthread_create(&x1, NULL, example, (void*) &value1);
pthread_create(&x2, NULL, example, (void*) &value2);
pthread_join(x1, NULL); pthread_join(x2, NULL); return 0; }
void *example(void *y1) { long add = 0;
long *y2 = (long *) (y1); Run
for (long i=0; i<1000000; i++) $> gcc -o p4 e.c
add = add + *y2; $> time ./p4
return NULL; } .....
Example 5: Advantage of Multiple Threads over Single
Thread
void *example(void *y1);
int main() {
pthread_t x1, x2;
int value1 = 5; int value2 = 10;
example((void *) &value1);
example((void *) &value2);
return 0; }
void *example(void *y1) { long add = 0;
long *y2 = (long *) (y1); Run
for (long i=0; i<1000000; i++) $> gcc -o p5 f.c
add = add + *y2; $> time ./p5
return NULL; } .....
Example 6: Advantage of Multiple Threads over Single
Thread
void *example(void *y1);
int main() {
pthread_t x1, x2;
int value1 = 5; int value2 = 10;
pthread_create(&x1, NULL, example, (void*) &value1);
pthread_join(x1, NULL);
pthread_create(&x2, NULL, example, (void*) &value2);
pthread_join(x2, NULL);
return 0; }
void *example(void *y1) { long add = 0;
Run
long *y2 = (long *) (y1);
$> gcc -o p6 g.c
for (long i=0; i<1000000; i++)
$> time ./p6
add = add + *y2;
.....
return NULL; }
Example 7: Multiple Threads with Return Value
void *example(void *y1);
int main() {
pthread_t x1, x2; int *res1, *res2;
int value1 = 5; int value2 = 10;
pthread_create(&x1, NULL, example, (void*) &value1);
pthread_create(&x2, NULL, example, (void*) &value2);
pthread_join(x1, (void**) &res1); pthread_join(x2, (void**) &res2);
printf(“Result 1 = %d\n”, *res1); printf(“Result 2 = %d\n”, *res2);
return 0; }
Run
void *example(void *y1) { int add = 0; $> gcc -o p7 h.c
int *y2 = (int *) (y1); $> ./p7
for (int i=0; i<100; i++) add = add + *y2; Segmentation fault
return (void*) add; }
Example 7: Multiple Threads with Return Value Contd...

................
pthread_join(x1, (void**) &res1); pthread_join(x2, (void**) &res2);
printf(“Result 1 = %d\n”, *res1); printf(“Result 2 = %d\n”, *res2);
free (res1); free(res2);
return 0; }
void *example(void *y1) { int add = 0;
int* result = malloc(sizeof(int));
int *y2 = (int *) (y1); Run
for (int i=0; i<100; i++) add = add + *y2; $> gcc -o p7 h.c
*result = add; $> ./p7
return (void*) result; } ......

You might also like