A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
main-test-sync.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "ns3/command-line.h"
8#include "ns3/config.h"
9#include "ns3/global-value.h"
10#include "ns3/log.h"
11#include "ns3/nstime.h"
12#include "ns3/ptr.h"
13#include "ns3/realtime-simulator-impl.h"
14#include "ns3/simulator.h"
15#include "ns3/string.h"
16
17#include <chrono> // seconds, milliseconds
18#include <thread> // sleep_for
19
20/**
21 * \file
22 * \ingroup core-examples
23 * \ingroup scheduler
24 * An example of scheduling events in a background thread.
25 *
26 * See \ref ns3::SimulatorImpl::ScheduleWithContext
27 */
28
29using namespace ns3;
30
31NS_LOG_COMPONENT_DEFINE("TestSync");
32
33namespace
34{
35
36/** Check that the event functions run in the intended order. */
37bool gFirstRun = false;
38
39/** An event method called many times from the background thread. */
40void
41inserted_function()
42{
43 NS_ASSERT(gFirstRun);
44 NS_LOG_UNCOND("inserted_function() called at " << Simulator::Now().GetSeconds() << " s");
45}
46
47/** An event method called many times from the main thread. */
48void
49background_function()
50{
51 NS_ASSERT(gFirstRun);
52 NS_LOG_UNCOND("background_function() called at " << Simulator::Now().GetSeconds() << " s");
53}
54
55/** An event method called once from the main thread. */
56void
57first_function()
58{
59 NS_LOG_UNCOND("first_function() called at " << Simulator::Now().GetSeconds() << " s");
60 gFirstRun = true;
61}
62
63/** Example class with a method for the background task. */
64class FakeNetDevice
65{
66 public:
67 /** Constructor. */
68 FakeNetDevice();
69 /** The thread entry point. */
70 void Doit3();
71};
72
73FakeNetDevice::FakeNetDevice()
74{
76}
77
78void
79FakeNetDevice::Doit3()
80{
82 std::this_thread::sleep_for(std::chrono::seconds(1));
83
84 for (uint32_t i = 0; i < 10000; ++i)
85 {
86 //
87 // Exercise the realtime relative now path
88 //
90 Seconds(0.0),
91 MakeEvent(&inserted_function));
92 std::this_thread::sleep_for(std::chrono::milliseconds(1));
93 }
94}
95
96/**
97 * Example use of std::thread.
98 *
99 * This example is a complete simulation.
100 * It schedules \c first_function and many executions of \c background_function
101 * to execute in the main (foreground) thread. It also launches a background
102 * thread with an instance of FakeNetDevice, which schedules many instances of
103 * \c inserted_function.
104 */
105void
106test()
107{
108 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
109
110 FakeNetDevice fnd;
111
112 //
113 // Make sure ScheduleNow works when the system isn't running
114 //
115 Simulator::ScheduleWithContext(0xffffffff, Seconds(0.0), MakeEvent(&first_function));
116
117 //
118 // drive the progression of m_currentTs at a ten millisecond rate from the main thread
119 //
120 for (double d = 0.; d < 14.999; d += 0.01)
121 {
122 Simulator::Schedule(Seconds(d), &background_function);
123 }
124
125 std::thread st3 = std::thread(&FakeNetDevice::Doit3, &fnd);
126
129
130 if (st3.joinable())
131 {
132 st3.join();
133 }
134
136}
137
138} // unnamed namespace
139
140int
141main(int argc, char* argv[])
142{
143 CommandLine cmd(__FILE__);
144 cmd.Parse(argc, argv);
145
146 while (true)
147 {
148 test();
149 }
150
151 return 0;
152}
Parse command-line arguments.
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition simulator.h:199
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
std::enable_if_t< std::is_member_pointer_v< MEM >, EventImpl * > MakeEvent(MEM mem_ptr, OBJ obj, Ts... args)
Make an EventImpl from class method members which take varying numbers of arguments.
Definition make-event.h:133
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
-ns3 Test suite for the ns3 wrapper script