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 // This example implements a simple producer/consumer schema,
7 // passing a bunch of items from one to the other
9 #include "simgrid/s4u.hpp"
13 namespace sg4 = simgrid::s4u;
15 XBT_LOG_NEW_DEFAULT_CATEGORY(sem_test, "Simple test of the semaphore");
17 const char* buffer; /* Where the data is exchanged */
18 sg4::SemaphorePtr sem_empty = sg4::Semaphore::create(1); /* indicates whether the buffer is empty */
19 sg4::SemaphorePtr sem_full = sg4::Semaphore::create(0); /* indicates whether the buffer is full */
21 static void producer(const std::vector<std::string>& args)
23 for (auto const& str : args) {
25 XBT_INFO("Pushing '%s'", str.c_str());
32 static void consumer()
38 XBT_INFO("Receiving '%s'", str.c_str());
45 int main(int argc, char **argv)
47 std::vector<std::string> args({"one", "two", "three", ""});
48 sg4::Engine e(&argc, argv);
49 e.load_platform(argc > 1 ? argv[1] : "../../platforms/two_hosts.xml");
50 sg4::Actor::create("producer", e.host_by_name("Tremblay"), producer, std::cref(args));
51 sg4::Actor::create("consumer", e.host_by_name("Jupiter"), consumer);