X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/42a689df45fca8334757bf342483b75ddf4a9835..417ed3b671abe3a71fa4106d23d0a432084cc207:/examples/cpp/synchro-semaphore/s4u-synchro-semaphore.cpp diff --git a/examples/cpp/synchro-semaphore/s4u-synchro-semaphore.cpp b/examples/cpp/synchro-semaphore/s4u-synchro-semaphore.cpp index 7acc1ac3d3..4040407811 100644 --- a/examples/cpp/synchro-semaphore/s4u-synchro-semaphore.cpp +++ b/examples/cpp/synchro-semaphore/s4u-synchro-semaphore.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -10,24 +10,23 @@ #include -XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category"); +namespace sg4 = simgrid::s4u; -const char* buffer; /* Where the data is exchanged */ -simgrid::s4u::SemaphorePtr sem_empty = simgrid::s4u::Semaphore::create(1); /* indicates whether the buffer is empty */ -simgrid::s4u::SemaphorePtr sem_full = simgrid::s4u::Semaphore::create(0); /* indicates whether the buffer is full */ +XBT_LOG_NEW_DEFAULT_CATEGORY(sem_test, "Simple test of the semaphore"); -static void producer(const std::vector& args) +static void producer(std::string& buffer, sg4::SemaphorePtr sem_empty, sg4::SemaphorePtr sem_full, + const std::vector& args) { for (auto const& str : args) { sem_empty->acquire(); XBT_INFO("Pushing '%s'", str.c_str()); - buffer = str.c_str(); + buffer = str; sem_full->release(); } XBT_INFO("Bye!"); } -static void consumer() +static void consumer(const std::string& buffer, sg4::SemaphorePtr sem_empty, sg4::SemaphorePtr sem_full) { std::string str; do { @@ -43,10 +42,16 @@ static void consumer() int main(int argc, char **argv) { std::vector args({"one", "two", "three", ""}); - simgrid::s4u::Engine e(&argc, argv); - e.load_platform("../../platforms/two_hosts.xml"); - simgrid::s4u::Actor::create("producer", simgrid::s4u::Host::by_name("Tremblay"), producer, std::cref(args)); - simgrid::s4u::Actor::create("consumer", simgrid::s4u::Host::by_name("Jupiter"), consumer); + sg4::Engine e(&argc, argv); + e.load_platform(argc > 1 ? argv[1] : "../../platforms/two_hosts.xml"); + + std::string buffer; /* Where the data is exchanged */ + auto sem_empty = sg4::Semaphore::create(1); /* indicates whether the buffer is empty */ + auto sem_full = sg4::Semaphore::create(0); /* indicates whether the buffer is full */ + + sg4::Actor::create("producer", e.host_by_name("Tremblay"), producer, std::ref(buffer), sem_empty, sem_full, + std::cref(args)); + sg4::Actor::create("consumer", e.host_by_name("Jupiter"), consumer, std::cref(buffer), sem_empty, sem_full); e.run(); return 0;