1 /* Copyright (c) 2007-2016. 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 /** @addtogroup MSG_examples
8 * - <b>maestro-set/maestro-set.cpp: Switch the system thread hosting our maestro</b>.
9 * That's a very advanced example in which we move the maestro thread to another process.
10 * Not many users need it (maybe only one, actually), but this example is also a regression test.
12 * This example is in C++ because we use C++11 threads to ensure that the feature is working as
13 * expected. You can still use that feature from a C code.
16 #include "simgrid/msg.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
22 std::thread::id root_id;
24 static void ensure_root_tid() {
25 std::thread::id this_id = std::this_thread::get_id();
26 xbt_assert(root_id == this_id, "I was supposed to be the main thread");
27 XBT_INFO("I am the main thread, as expected");
29 static void ensure_other_tid() {
30 std::thread::id this_id = std::this_thread::get_id();
31 xbt_assert(this_id != root_id, "I was NOT supposed to be the main thread");
32 XBT_INFO("I am not the main thread, as expected");
37 static int sender(int argc, char *argv[])
41 msg_task_t task_la = MSG_task_create("Some task", 0.0, 10e8, NULL);
42 MSG_task_send(task_la, "some mailbox");
47 static int receiver(int argc, char *argv[])
51 msg_task_t task_la = NULL;
52 xbt_assert(MSG_task_receive(&task_la,"some mailbox") == MSG_OK);
53 XBT_INFO("Task received");
54 MSG_task_destroy(task_la);
59 static void maestro(void* data)
62 MSG_process_create("receiver",&receiver,NULL,MSG_host_by_name("Jupiter"));
67 int main(int argc, char *argv[])
69 root_id = std::this_thread::get_id();
71 SIMIX_set_maestro(maestro, NULL);
72 MSG_init(&argc, argv);
75 XBT_CRITICAL("Usage: %s platform_file\n", argv[0]);
76 xbt_die("example: %s msg_platform.xml\n",argv[0]);
79 MSG_create_environment(argv[1]);
81 /* Become one of the simulated process.
83 * This must be done after the creation of the platform because we are depending attaching to a host.*/
84 MSG_process_attach("sender", NULL, MSG_host_by_name("Tremblay"), NULL);
87 // Execute the sender code:
88 const char* subargv[3] = { "sender", "Jupiter", NULL };
89 sender(2, (char**) subargv);
91 MSG_process_detach(); // Become root thread again