1 /* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include <mutex> /* std::mutex and std::lock_guard */
7 #include <simgrid/s4u.hpp> /* All of S4U */
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
11 simgrid::s4u::MutexPtr mtx = nullptr;
12 simgrid::s4u::ConditionVariablePtr cv = nullptr;
15 static void competitor(int id)
17 XBT_INFO("Entering the race...");
18 std::unique_lock<simgrid::s4u::Mutex> lck(*mtx);
20 auto now = simgrid::s4u::Engine::get_clock();
21 if (cv->wait_until(lck, now + (id+1)*0.25) == std::cv_status::timeout) {
22 XBT_INFO("Out of wait_until (timeout)");
25 XBT_INFO("Out of wait_until (YAY!)");
33 XBT_INFO("Are you ready? ...");
34 simgrid::s4u::this_actor::sleep_for(3);
35 std::unique_lock<simgrid::s4u::Mutex> lck(*mtx);
36 XBT_INFO("Go go go!");
41 static void main_actor()
43 mtx = simgrid::s4u::Mutex::create();
44 cv = simgrid::s4u::ConditionVariable::create();
46 auto host = simgrid::s4u::this_actor::get_host();
47 for (int i = 0; i < 10; ++i)
48 simgrid::s4u::Actor::create("competitor", host, competitor, i);
49 simgrid::s4u::Actor::create("go", host, go);
52 int main(int argc, char* argv[])
54 simgrid::s4u::Engine e(&argc, argv);
55 e.load_platform("../../platforms/small_platform.xml");
57 simgrid::s4u::Actor::create("main", e.host_by_name("Tremblay"), main_actor);