Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics: define and use the sg4 namespace as a shortcut to simgrid::s4u
[simgrid.git] / examples / s4u / actor-yield / s4u-actor-yield.cpp
1 /* Copyright (c) 2017-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid/s4u.hpp>
7 namespace sg4 = simgrid::s4u;
8
9 /* This example does not much: It just spans over-polite actor that yield a large amount
10  * of time before ending.
11  *
12  * This serves as an example for the sg4::this_actor::yield() function, with which an actor can request
13  * to be rescheduled after the other actor that are ready at the current timestamp.
14  *
15  * It can also be used to benchmark our context-switching mechanism.
16  */
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_yield, "Messages specific for this s4u example");
18 /* Main function of the Yielder actor */
19 class yielder {
20   long number_of_yields;
21
22 public:
23   explicit yielder(std::vector<std::string> args) { number_of_yields = std::stol(args[1]); }
24   void operator()() const
25   {
26     for (int i = 0; i < number_of_yields; i++)
27       sg4::this_actor::yield();
28     XBT_INFO("I yielded %ld times. Goodbye now!", number_of_yields);
29   }
30 };
31
32 int main(int argc, char* argv[])
33 {
34   sg4::Engine e(&argc, argv);
35
36   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
37                        "\tExample: %s platform.xml deployment.xml\n",
38              argv[0], argv[0]);
39
40   e.load_platform(argv[1]);             /* Load the platform description */
41   e.register_actor<yielder>("yielder"); /* Register the class representing the actors */
42
43   e.load_deployment(argv[2]);
44
45   e.run(); /* - Run the simulation */
46
47   return 0;
48 }