1 /* Copyright (c) 2006-2020. 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);
36 std::unique_lock<simgrid::s4u::Mutex> lck(*mtx);
37 XBT_INFO("Go go go!");
43 static void main_actor()
45 mtx = simgrid::s4u::Mutex::create();
46 cv = simgrid::s4u::ConditionVariable::create();
48 auto host = simgrid::s4u::this_actor::get_host();
49 for (int i = 0; i < 10; ++i)
50 simgrid::s4u::Actor::create("competitor", host, competitor, i);
51 simgrid::s4u::Actor::create("go", host, go);
54 int main(int argc, char* argv[])
56 simgrid::s4u::Engine e(&argc, argv);
57 e.load_platform("../../platforms/small_platform.xml");
59 auto host = simgrid::s4u::Host::by_name("Tremblay");
60 simgrid::s4u::Actor::create("main", host, main_actor);