Discrete-event simulation in C++20 using coroutines

SimCpp20

SimCpp20 is a discrete-event simulation framework for C++20. It is similar to SimPy and aims to be easy to set up and use.

Processes are defined as functions receiving simcpp20::simulation & as their first argument and returning simcpp20::process. Each process is executed as a coroutine. Thus, this framework requires C++20. To compile a simulation, use g++ -Wall -std=c++20 -fcoroutines example.cpp simcpp20.cpp -o example. For this to work, g++ must be on version 10 (you can try g++-10 too). A short example simulating two clocks ticking in different time intervals looks like this:

#include <coroutine>
#include <iostream>

#include "simcpp20.hpp"

simcpp20::process clock_proc(simcpp20::simulation &sim, std::string name,
                            double delay) {
  while (true) {
    std::cout << name << " " << sim.now() << std::endl;
    co_await sim.timeout(delay);
  }
}

int main() {
  simcpp20::simulation sim;
  clock_proc(sim, "fast", 1);
  clock_proc(sim, "slow", 2);
  sim.run_until(5);
}

When run, the following output is generated:

fast 0
slow 0
fast 1
slow 2
fast 2
fast 3
slow 4
fast 4
fast 5

Copyright and License

Copyright © 2021 Felix Schütz.

Licensed under the MIT License. See the LICENSE file for details.

Similar Resources

🧵 Fast and easy multithreading for React Native using JSI

🧵 Fast and easy multithreading for React Native using JSI

react-native-multithreading 🧵 Fast and easy multithreading for React Native using JSI. Installation npm install react-native-multithreading npx pod-i

Dec 31, 2022

Exploration of x86-64 ISA using speculative execution.

Haruspex /həˈrʌspeks/ A religious official in ancient Rome who predicted the future or interpreted the meaning of events by examining the insides of b

Nov 21, 2022

Thread pool - Thread pool using std::* primitives from C++17, with optional priority queue/greenthreading for POSIX.

thread_pool Thread pool using std::* primitives from C++11. Also includes a class for a priority thread pool. Requires concepts and C++17, including c

Dec 30, 2022

Thread-pool - Thread pool implementation using c++11 threads

Thread-pool - Thread pool implementation using c++11 threads

Table of Contents Introduction Build instructions Thread pool Queue Submit function Thread worker Usage example Use case#1 Use case#2 Use case#3 Futur

Dec 27, 2022

Parallel implementation of Dijkstra's shortest path algorithm using MPI

Parallel implementation of Dijkstra's shortest path algorithm using MPI

Jan 21, 2022

Sysmon event simulation utility which can be used to simulate the attacks to generate the Sysmon Event logs for testing the EDR detections and correlation rules by Blue teams.

SysmonSimulator SysmonSimulator is an Open source Windows event simulation utility created in C language, that can be used to simulate most of the att

Dec 25, 2022

Research on Event Accumulator Settings for Event-Based SLAM

Research on Event Accumulator Settings for Event-Based SLAM

Research on Event Accumulator Settings for Event-Based SLAM This is the source code for paper "Research on Event Accumulator Settings for Event-Based

Dec 21, 2022

A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Using Pro-micro control.

A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Using Pro-micro control.

N.A.G.E.K.I. A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Using Pro-micro control. 中文版

Dec 8, 2022

A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Using Pro-micro control.

A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Using Pro-micro control.

N.A.G.E.K.I. PLEASE CHECK Main Project A cheap,simple,Ongeki controller Use Keyboard Simulation and Mouse Simulation to controller the ongeki game. Us

Dec 30, 2021

Assignment skeleton for course on Discrete Differential Geometry (15-458/858)

ddg-exercises This repo contains C++ skeleton code for course assignments from Discrete Differential Geometry (15-458/858). For the JavaScript version

Dec 12, 2022

Discrete Conformal Equivalence of Polyhedral Surfaces

Discrete Conformal Equivalence of Polyhedral Surfaces

Discrete Conformal Equivalence of Polyhedral Surfaces C++ demo for "Discrete Conformal Equivalence of Polyhedral Surfaces" by Mark Gillespie, Boris Sp

Dec 6, 2022

A global planner plugin for ROS navigation stack, in which A* search on a discrete gneralized Voronoi diagram (GVD) is implemented.

voronoi_planner This repo contains a global planner plugin for ROS navigation stack, in which A* search on a discrete gneralized Voronoi diagram (GVD)

Nov 30, 2022

Parallel library for approximate inference on discrete Bayesian networks

baylib C++ library Baylib is a parallel inference library for discrete Bayesian networks supporting approximate inference algorithms both in CPU and G

Dec 7, 2022

Analysing and implementation of lossless data compression techniques like Huffman encoding and LZW was conducted along with JPEG lossy compression technique based on discrete cosine transform (DCT) for Image compression.

PROJECT FILE COMPRESSION ALGORITHMS - Huffman compression LZW compression DCT Aim of the project - Implement above mentioned compression algorithms an

Dec 14, 2021

A static C++ library for the generation of discrete functions on a box-shaped domain

A static C++ library for the generation of discrete functions on a box-shaped domain

A static C++ library for the generation of discrete functions on a box-shaped domain. This is especially suited for the discretization of signed distance fields.

Nov 29, 2022

FFTW is a free collection of fast C routines for computing the Discrete Fourier Transform in one or more dimensions

FFTW is a free collection of fast C routines for computing the Discrete Fourier Transform in one or more dimensions

Aug 12, 2022

Real-time multi-physics simulation with an emphasis on medical simulation.

Real-time multi-physics simulation with an emphasis on medical simulation.

Introduction SOFA is an open source framework primarily targeted at real-time simulation, with an emphasis on medical simulation. It is mainly intende

Dec 22, 2022

A PIC/FLIP fluid simulation based on the methods found in Robert Bridson's "Fluid Simulation for Computer Graphics"

A PIC/FLIP fluid simulation based on the methods found in Robert Bridson's

GridFluidSim3d This program is an implementation of a PIC/FLIP liquid fluid simulation written in C++11 based on methods described in Robert Bridson's

Dec 8, 2022

Modern concurrency for C++. Tasks, executors, timers and C++20 coroutines to rule them all

concurrencpp, the C++ concurrency library concurrencpp is a tasking library for C++ allowing developers to write highly concurrent applications easily

Jan 3, 2023
Comments
  • Double Free of std::coroutine_handle

    Double Free of std::coroutine_handle

    Apparently, when using the value_event class the system incurs in a double free error when exiting the program (i.e., all processes have been stopped).

    value_process(64525,0x100f6c580) malloc: *** error for object 0x600001844050: pointer being freed was not allocated
    

    Unfortunately I cannot go too much in depth for debugging, but it seems that the error is related to the following destructor (and possibly to this phenomenon) in event.hpp:

     class data {
      public:
        ...
        /// Destructor.
        virtual ~data() {
          for (auto &handle : handles_) {
            handle.destroy();
          }
        }
    }
    

    A possible workaround might be to use a std::suspend_always instead of a std::suspend_never for the final_suspend method, however I am not able to understand the implications for the simulation.

    My system is a MacOS 12.3.1 running on Arm M1 Pro. The code has been compiled with g++-11 (Homebrew GCC 11.2.0_3) 11.2.0.

  • Double free fix bug #1

    Double free fix bug #1

    This should work, sorry for the inconvenience of the previous pull request that was invalid since I advanced some developments in the main branch. This PR fixes the problem with the double free.

C++20 coroutines-based cooperative multitasking library

?? Coop Coop is a C++20 coroutines-based library to support cooperative multitasking in the context of a multithreaded application. The syntax will be

Dec 9, 2022
Header-Only C++20 Coroutines library

CPP20Coroutines Header-Only C++20 Coroutines library This repository aims to demonstrate the capabilities of C++20 coroutines. generator Generates val

Aug 15, 2022
Open source PHP extension for Async IO, Coroutines and Fibers

Swoole is an event-driven asynchronous & coroutine-based concurrency networking communication engine with high performance written in C++ for PHP. Ope

Jan 4, 2023
Cppcoro - A library of C++ coroutine abstractions for the coroutines TS

CppCoro - A coroutine library for C++ The 'cppcoro' library provides a large set of general-purpose primitives for making use of the coroutines TS pro

Dec 30, 2022
Coro - Single-header library facilities for C++2a Coroutines

coro This is a collection of single-header library facilities for C++2a Coroutines. coro/include/ co_future.h Provides co_future<T>, which is like std

Dec 6, 2022
C++14 asynchronous allocation aware futures (supporting then, exception handling, coroutines and connections)
C++14 asynchronous allocation aware futures (supporting then, exception handling, coroutines and connections)

Continuable is a C++14 library that provides full support for: lazy async continuation chaining based on callbacks (then) and expression templates, ca

Dec 20, 2022
Sqrt OS is a simulation of an OS scheduler and memory manager using different scheduling algorithms including Highest Priority First (non-preemptive), Shortest Remaining Time Next, and Round Robin
Sqrt OS is a simulation of an OS scheduler and memory manager using different scheduling algorithms including Highest Priority First (non-preemptive), Shortest Remaining Time Next, and Round Robin

A CPU scheduler determines an order for the execution of its scheduled processes; it decides which process will run according to a certain data structure that keeps track of the processes in the system and their status.

Sep 7, 2022
OpenCL based GPU accelerated SPH fluid simulation library

libclsph An OpenCL based GPU accelerated SPH fluid simulation library Can I see it in action? Demo #1 Demo #2 Why? Libclsph was created to explore the

Jul 27, 2022
KRATOS Multiphysics ("Kratos") is a framework for building parallel, multi-disciplinary simulation software
KRATOS Multiphysics (

KRATOS Multiphysics ("Kratos") is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.

Dec 29, 2022
A novel technique to communicate between threads using the standard ETHREAD structure
A novel technique to communicate between threads using the standard ETHREAD structure

??️ dearg-thread-ipc-stealth Usage There are two main exported methods, one to read from another thread, and another to serve the content to another t

Nov 10, 2022