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");
10 namespace sg4 = simgrid::s4u;
12 sg4::MutexPtr mtx = nullptr;
13 sg4::ConditionVariablePtr cv = nullptr;
16 static void competitor(int id)
18 XBT_INFO("Entering the race...");
19 std::unique_lock lck(*mtx);
21 auto now = sg4::Engine::get_clock();
22 if (cv->wait_until(lck, now + (id+1)*0.25) == std::cv_status::timeout) {
23 XBT_INFO("Out of wait_until (timeout)");
26 XBT_INFO("Out of wait_until (YAY!)");
34 XBT_INFO("Are you ready? ...");
35 sg4::this_actor::sleep_for(3);
36 std::unique_lock lck(*mtx);
37 XBT_INFO("Go go go!");
42 static void main_actor()
44 mtx = sg4::Mutex::create();
45 cv = sg4::ConditionVariable::create();
47 auto host = sg4::this_actor::get_host();
48 for (int i = 0; i < 10; ++i)
49 sg4::Actor::create("competitor", host, competitor, i);
50 sg4::Actor::create("go", host, go);
53 int main(int argc, char* argv[])
55 sg4::Engine e(&argc, argv);
56 e.load_platform("../../platforms/small_platform.xml");
58 sg4::Actor::create("main", e.host_by_name("Tremblay"), main_actor);