This GitHub repository contains examples and explanations of basic POSIX Threads programming using C programming language.
POSIX Threads (also known as Pthreads) is a POSIX standard for threads programming. It defines a set of C programming language constructs that can be used to create and manipulate threads. Threads are lightweight procedures that can run concurrently within a single process. By using threads, we can divide a complex task into smaller parts, which can be executed in parallel, thereby improving the overall performance of the application.
- Overview
- Thread Creation
pthread_create.c
This file demonstrates how to create a new thread using the pthread_create() function.pthread_join.c
This file demonstrates how to wait for a thread to complete its execution using the pthread_join() function.pthread_multiple_threads.c
This file demonstrates how to create and join multiple threads using a for loop.
- Mutexes
pthread_shared_vars.c
This file demonstrates how threads share memory.pthread_race_condition.c
This file demonstrates how the share memory may result in race conditions.pthread_mutex.c
This file demonstrates how to use a mutex to protect a critical section of code from concurrent access by multiple threads.
- Arguments and Return Values in thread functions
pthread_return_value.c
This file demonstrates how to return values from the thread functions.pthread_exit.c
This file demonstrates how to use pthread_exit to return values.pthread_thread_arg.c
This file demonstrates how to send arguments in thread functions.
- Try Lock
pthread_trylock.c
This file demonstrates how mutex lock is different from try lock.
- Condition Variables
pthread_cond_var.c
This file demonstrates how condition variables can be used.pthread_cond_broadcast.c
This file demonstrates how broadcast signal can be used.
- Finding array sum parallely
pthread_sum_array.c
This file demonstrates how to sum an array parallely using two threads.pthread_sum_array_mutex.c
This file demonstrates how to sum an array parallely using mutex.
- Stock Simulator
stocksimulator1.c
This is a program that simulates a simple stock market where multiple threads (representing stock brokers) try to buy stocks until they are no longer available. It uses a mutex to synchronize access to the shared stock data and outputs a summary of how many stocks each broker bought.stocksimulator2.c
The second program builds on the first one by adding a stock exchange thread that periodically updates the stock price. The stock brokers only buy the stock if the price falls below a certain threshold, and they wait until the price drops before making a purchase using condition variables.
To compile and run the code in this repository, you will need:
A C compiler (such as gcc) A POSIX Threads library (such as pthreads)
Go through each topic and run the code using:
On Windows:
gcc file_name.c -fopenmp
On Unix/Linux:
gcc file_name.c
This repository provides a starting point for learning about basic POSIX Threads programming. The examples provided here can be used as a reference for implementing multithreaded applications in C. If you have any questions or suggestions, feel free to open an issue or submit a pull request.