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 // 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 static void producer(std::string* buffer, sg4::SemaphorePtr sem_empty, sg4::SemaphorePtr sem_full,
18 const std::vector<std::string>& args)
20 for (auto const& str : args) {
22 XBT_INFO("Pushing '%s'", str.c_str());
29 static void consumer(const std::string* buffer, sg4::SemaphorePtr sem_empty, sg4::SemaphorePtr sem_full)
35 XBT_INFO("Receiving '%s'", str.c_str());
42 int main(int argc, char **argv)
44 std::vector<std::string> args({"one", "two", "three", ""});
45 sg4::Engine e(&argc, argv);
46 e.load_platform(argc > 1 ? argv[1] : "../../platforms/two_hosts.xml");
48 std::string buffer; /* Where the data is exchanged */
49 auto sem_empty = sg4::Semaphore::create(1); /* indicates whether the buffer is empty */
50 auto sem_full = sg4::Semaphore::create(0); /* indicates whether the buffer is full */
52 sg4::Actor::create("producer", e.host_by_name("Tremblay"), producer, &buffer, sem_empty, sem_full, std::cref(args));
53 sg4::Actor::create("consumer", e.host_by_name("Jupiter"), consumer, &buffer, sem_empty, sem_full);