1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
/* Copyright (c) 2006-2025. 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. */
// This example implements a simple producer/consumer schema,
// passing a bunch of items from one to the other
#include "simgrid/s4u.hpp"
#include <memory>
namespace sg4 = simgrid::s4u;
XBT_LOG_NEW_DEFAULT_CATEGORY(sem_test, "Simple test of the semaphore");
static void producer(std::string* buffer, sg4::SemaphorePtr sem_empty, sg4::SemaphorePtr sem_full,
const std::vector<std::string>& args)
{
for (auto const& str : args) {
sem_empty->acquire();
XBT_INFO("Pushing '%s'", str.c_str());
*buffer = str;
sem_full->release();
}
XBT_INFO("Bye!");
}
static void consumer(const std::string* buffer, sg4::SemaphorePtr sem_empty, sg4::SemaphorePtr sem_full)
{
std::string str;
do {
sem_full->acquire();
str = *buffer;
XBT_INFO("Receiving '%s'", str.c_str());
sem_empty->release();
} while (str != "");
XBT_INFO("Bye!");
}
int main(int argc, char **argv)
{
std::vector<std::string> args({"one", "two", "three", ""});
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 */
e.host_by_name("Tremblay")->add_actor("producer", producer, &buffer, sem_empty, sem_full, std::cref(args));
e.host_by_name("Jupiter")->add_actor("consumer", consumer, &buffer, sem_empty, sem_full);
e.run();
return 0;
}
|