0% found this document useful (0 votes)
9 views11 pages

Understanding Multithreaded Programming

The document discusses the differences between processes and threads, highlighting that threads share resources while having their own program counter and stack. It explains the benefits of multithreading, such as faster creation and context switching, and provides examples of multithreading in applications like browsers and word processors. Additionally, it includes C code snippets demonstrating thread creation and management using the pthread library.

Uploaded by

dmxmohankumarp
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)
9 views11 pages

Understanding Multithreaded Programming

The document discusses the differences between processes and threads, highlighting that threads share resources while having their own program counter and stack. It explains the benefits of multithreading, such as faster creation and context switching, and provides examples of multithreading in applications like browsers and word processors. Additionally, it includes C code snippets demonstrating thread creation and management using the pthread library.

Uploaded by

dmxmohankumarp
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

Multi-threaded programming

What are the differences between process and thread?

Threads are not independent from each other unlike processes. As a result, threads shares with other threads
their code section, data section and OS resources like open files and signals. But, like processes, a thread has its
own program counter (PC), a register set, and a stack space.

Multithreading: This is all about a single process split into multiple threads.
Parallel Programming: This is all about multiple tasks running on multiple cores simultaneously.
Asynchronous Programming: This is all about a single thread initiating multiple tasks without waiting for each to
complete.
Why Multithreading?
Threads are popular way to improve application through parallelism. For example, in a browser, multiple tabs can be different
threads. MS word uses multiple threads, one thread to format the text, other thread to process inputs, etc.
Threads operate faster than processes due to following reasons:

1) Thread creation is much faster.

2) Context switching between threads is much faster.

3) Threads can be terminated easily

4) Communication between threads is faster.


#include <stdio.h> Please note that the program may compile
#include <stdlib.h> only with C compilers with pthread library.
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>
• In main(), we declare a variable called thread_id, which is of
// A normal C function that is executed as a thread type pthread_t, which is an integer used to identify the thread
// when its name is specified in pthread_create() in the system.
void *myThreadFun(void *vargp) • After declaring thread_id, we call pthread_create() function
{ to create a thread.
sleep(1); • pthread_create() takes 4 arguments.
printf("Printing SPC Quiz from Thread \n"); • The first argument is a pointer to thread_id which is set by this
return NULL; function.
} • The second argument specifies attributes. If the value is NULL,
then default attributes shall be used.
int main() • The third argument is name of function to be executed for the
{ thread to be created.
pthread_t thread_id; • The fourth argument is used to pass arguments to the
printf("Before Thread\n"); function, myThreadFun.
pthread_create(&thread_id, NULL, myThreadFun, NULL);
pthread_join(thread_id, NULL); • The pthread_join() function for threads is the equivalent of
printf("After Thread\n"); wait() for processes.
exit(0); • A call to pthread_join blocks the calling thread until the thread
} with identifier equal to the first argument terminates.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int g = 0; // Let us create a global variable to change it in threads
// The function to be executed by all threads
void *myThreadFun(void *vargp) {
// Store the value argument passed to this thread
int *myid = (int *)vargp;
// Let us create a static variable to observe its changes
static int s = 0;
// Change static and global variables
++s; ++g;
// Print the argument, static and global variables
printf("Thread ID: %d, Static: %d, Global: %d\n", *myid, ++s, ++g); }
int main() {
int i;
pthread_t tid;
// Let us create three threads
for (i = 0; i < 3; i++)
pthread_create(&tid, NULL, myThreadFun, (void *)&tid);
pthread_exit(NULL);
return 0;
}
#include <pthread.h> Example 3
#include <stdio.h>
#include <stdlib.h>
static int i;
void* myworker(void* arg) {
//delay loop to strenghthen the fact that main thread
//is waiting for the child thread to finish
for (int p = 0; p < 10000000; p++) ;
i = 5;
return NULL; }
int main() {
pthread_t id;
i = 3;
printf("In the main thread, i= %d \n", i);
printf("Creating a new thread where we will change the value of i and we will print the changed value here\n");
int failed = pthread_create(&id, NULL, myworker, NULL);
if (failed) {
printf("can't create a child thread, process killed\n"); return 0; }
else {
printf("Successfully created a child thread\n");
}
//wait for the child thread to finish
pthread_join(id, NULL);
printf("Child thread has finished changing the value of i\n");
printf("i is now %d \n", i);
return 0; }

You might also like