Skip to content

rafixcs/SharedMemoryExamples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SharedMemory C++ Project

This project demonstrates various C++ threading concepts and shared memory operations using Boost.Interprocess library.

Overview

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.

Features Demonstrated

1. Basic Threading Examples

Simple Thread Creation (testPrintThread)

  • Creates two threads that print messages
  • Demonstrates basic thread creation and joining
  • Uses std::thread for concurrent execution

Thread-Safe Counter (testCounterThreading)

  • Implements a shared counter with mutex protection
  • Uses std::lock_guard for automatic mutex management
  • Demonstrates race condition prevention

Producer-Consumer Pattern (testProducerConsumerThreading)

  • Implements a classic producer-consumer pattern
  • Uses std::condition_variable for thread synchronization
  • Producer generates data, consumer processes it
  • Demonstrates thread communication and coordination

Thread Pool Pattern (testPoolThreading)

  • Creates multiple worker threads with different workloads
  • Each thread processes a specific amount of work
  • Shows how to manage multiple threads efficiently

Async and Future (testAsyncAndFutureThreading)

  • Uses std::async and std::future for asynchronous operations
  • Demonstrates non-blocking computation
  • Shows how to retrieve results from asynchronous tasks

2. Shared Memory Operations

The main functionality focuses on inter-process communication using shared memory:

Writer Process (writer)

  • 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

Reader Process (reader)

  • Opens the existing shared memory object
  • Reads data from the shared memory
  • Displays the received message
  • Cleans up the shared memory object

Synchronized Communication

  • 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

Dependencies

  • C++11 or later - For threading support
  • Boost.Interprocess - For shared memory operations
  • Standard C++ Library - For threading primitives

Building the Project

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

Running the Program

./SharedMemory

Expected Output

The program will demonstrate:

  1. Writer process creating shared memory and writing data
  2. Reader process waiting for shared memory to become available
  3. Successful data transfer between processes
  4. Cleanup of shared memory resources

Code Structure

  • 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

Key Concepts Demonstrated

  1. Thread Safety: Mutex usage and lock guards
  2. Thread Synchronization: Condition variables and futures
  3. Inter-Process Communication: Shared memory objects
  4. Resource Management: Proper cleanup of shared resources
  5. Error Handling: Exception handling for shared memory operations

Notes

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published