Condition variable one type of thread synchronization mechanism.
By using condition variable we can tell one thread that when to execute.
using pthread_cond_signal pass the signal to another thread, this signal can be catch by another thread using pthread_cond_wait.
A Blog to learn Software Technologies like C, Golang and Java
Showing posts with label POSIX Thread. Show all posts
Showing posts with label POSIX Thread. Show all posts
5. Pthread Mutex in Linux
Mutex is used to synchronize the thread.
What would be the final value of globalCount in below program?
What would be the final value of globalCount in below program?
4. Thread joining in Linux
Program which contain in the post this and this main doesn't wait for thread finishes their work.
We can make the main thread wait for the child thread to finish their work with the help of thread join mechanism.
We need to set thread as joinable by using thread attribute.
We can make the main thread wait for the child thread to finish their work with the help of thread join mechanism.
We need to set thread as joinable by using thread attribute.
3. Passing arguments to thread in Linux
Program contain in the previous thread post tell how to pass single argument to thread handler function.
If you want to pass two or more variable to thread handler then need to define structure globally then pass the structure variable as argument when creating thread with the help of pthread_create.
If you want to pass two or more variable to thread handler then need to define structure globally then pass the structure variable as argument when creating thread with the help of pthread_create.
2. Thread creation in Linux
Main program implicitly create the starting thread. Other thread can be created by programmer with the help of pthread_create API.
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
1. Introduction to thread in Linux
What is thread?
Thread is the independent piece of code executed parallel y when main program executed.
Why Thread ?
Consider you are creating student report program in C, It should contain number of students written final exam, and their name and total mark.
What actually you do?
Answer is this . You will write main program which find number of students, list their name and total mark. So step by step you do.
But if you use thread,
main program list students name and total mark. same time thread will find total number of students. So we can save some time if we use thread.
Threading is an example of parallel programming.
Working environment :
Here used pthread (POSIX standard thread) library under Ubuntu(Linux).
gcc compiler used to compile the program
Compilations :
There is need to add pthread linker when compiling thread program with the help of gcc compiler.
gcc -o <output> <input.c> -lpthread
Subscribe to:
Posts (Atom)