1 /* Copyright (c) 2006-2023. 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;
15 static void worker_fun(sg4::ConditionVariablePtr cv, sg4::MutexPtr mutex)
17 std::unique_lock lock(*mutex);
19 XBT_INFO("Start processing data which is '%s'.", data.c_str());
20 data += " after processing";
22 // Send data back to main()
23 XBT_INFO("Signal to master that the data processing is completed, and exit.");
29 static void master_fun()
31 auto mutex = sg4::Mutex::create();
32 auto cv = sg4::ConditionVariable::create();
33 data = "Example data";
34 auto worker = sg4::Actor::create("worker", sg4::Host::by_name("Jupiter"), worker_fun, cv, mutex);
36 // wait for the worker
37 cv->wait(std::unique_lock<sg4::Mutex>(*mutex), []() { return done; });
38 XBT_INFO("data is now '%s'.", data.c_str());
43 int main(int argc, char** argv)
45 sg4::Engine e(&argc, argv);
46 e.load_platform("../../platforms/two_hosts.xml");
47 sg4::Actor::create("main", e.host_by_name("Tremblay"), master_fun);