This project demonstrates various C++ threading concepts and shared memory operations using Boost.Interprocess library.
The main.cpp file contains multiple examples showcasing different threading patterns and inter-process communication techniques. The main focus is on demonstrating shared memory operations between processes using Boost.Interprocess.
- Creates two threads that print messages
- Demonstrates basic thread creation and joining
- Uses std::threadfor concurrent execution
- Implements a shared counter with mutex protection
- Uses std::lock_guardfor automatic mutex management
- Demonstrates race condition prevention
- Implements a classic producer-consumer pattern
- Uses std::condition_variablefor thread synchronization
- Producer generates data, consumer processes it
- Demonstrates thread communication and coordination
- Creates multiple worker threads with different workloads
- Each thread processes a specific amount of work
- Shows how to manage multiple threads efficiently
- Uses std::asyncandstd::futurefor asynchronous operations
- Demonstrates non-blocking computation
- Shows how to retrieve results from asynchronous tasks
The main functionality focuses on inter-process communication using shared memory:
- Creates a shared memory object named "MySharedMemory"
- Allocates 1024 bytes of shared memory
- Writes data ("Hello from writer!") to the shared memory
- Uses Boost.Interprocess library for cross-platform shared memory
- Opens the existing shared memory object
- Reads data from the shared memory
- Displays the received message
- Cleans up the shared memory object
- write_with_delay: Writer with 1-second delay
- read_with_wait: Reader that waits and retries if shared memory is not available
- Demonstrates proper synchronization between writer and reader processes
- C++11 or later - For threading support
- Boost.Interprocess - For shared memory operations
- Standard C++ Library - For threading primitives
This project uses CMake for building. Make sure you have Boost libraries installed:
# Install Boost (Ubuntu/Debian)
sudo apt-get install libboost-all-dev
# Build the project
mkdir build
cd build
cmake ..
make./SharedMemoryThe program will demonstrate:
- Writer process creating shared memory and writing data
- Reader process waiting for shared memory to become available
- Successful data transfer between processes
- Cleanup of shared memory resources
- Global Variables: Shared state for threading examples
- Threading Functions: Various threading pattern implementations
- Shared Memory Functions: Inter-process communication using Boost.Interprocess
- Main Function: Orchestrates the shared memory demonstration
- Thread Safety: Mutex usage and lock guards
- Thread Synchronization: Condition variables and futures
- Inter-Process Communication: Shared memory objects
- Resource Management: Proper cleanup of shared resources
- Error Handling: Exception handling for shared memory operations
- The shared memory object is automatically cleaned up after reading
- The program includes retry logic for robust shared memory access
- All threading examples are for educational purposes and demonstrate best practices
- The project showcases both basic threading concepts and advanced inter-process communication