Welcome! This repository contains a collection of C++ programs developed to explore and simulate different concurrency scenarios using both threads (pthread and ) and processes (fork()), along with mechanisms like mutexes and shared memory.
These programs were created with academic and experimental purposes in mind. Each simulation showcases how different concurrency models behave, especially when dealing with shared data and potential race conditions. They're great for understanding the challenges and tools available when writing concurrent code in C++.
-
hilos.cpp
Launches three threads, each printing its own message with a specific delay and number of repetitions. Itβs a minimal and friendly example to get started with std::thread -
hilos-arreglo.cpp
Similar to hilos.cpp, but this time the threads are managed using an array. Demonstrates how to work with multiple threads in a more scalable way.
- programa-sinmutex.cpp
Spawns 15 threads that each print messages multiple times, with random delays and no synchronization. This version intentionally omits mutexes to highlight how console outputs can become interleaved when threads arenβt synchronized.
- programa-conmutex.cpp
A refined version of the above program using a std::mutex to protect the output section. It prevents message mixing and ensures cleaner, thread-safe console output.
-
concurrencia-hilos.cpp
Simulates two types of threads: one that increments a shared variable and another that reads it. A mutex is used to avoid race conditions. Each thread sleeps for a random duration before executing its operation. -
concurrencia-procesos.cpp
Simulates concurrency using processes (fork()), where each process has its own copy of a variable. Demonstrates the lack of shared memory and how this affects data sharing between processes. -
concurrencia-procesos-memoriacomp.cpp
An improved version of the previous program that uses shared memory (mmap) to allow processes to increment and read a common variable. This version provides a realistic approach to inter-process communication and shared state.