Discussion:
Thread-unsafe invocation!
vahid Saber
2013-07-09 10:25:56 UTC
Permalink
Sorry if the question might be trivial, still can be interesting. Your kind
comments will be very valuable to me:


1. I am trying to pause and resume the simulation flow using the
messages received from outside ns3.
2. I want the ns3 pause until it receives information from an external
entity.
3. I have setup the infrastructure by creating a broker having the
following functions whose names speak for themselves:


*///////// BROKER*
//this is the first function and it uses boost::asio library to set up
//a connection and start reading(a thread is branched for this purpose)
bool Broker::*start()* {
m_universal = 0;
//connect and start reading
if(!m_cnn.connect(m_simmobility_address, m_simmobility_port, m_session))
{
return false;
}

//start a thread for io_service_run
m_io_service_runner = boost::thread(&Broker::run_io_service,
this, boost::ref(m_io_service));
std::cout << "Broker::start returning" << std::endl;
Simulator::ScheduleNow(&Broker::pause, this);
return true;
}

//I figured this would pause the simulation. I hope I was right.??
void Broker::*pause()*
{
boost::unique_lock<boost::mutex> lock(m_pause_mutex);
{
std::cout << "pausing the simulation" << std::endl;
m_cond_cnn.wait(lock);
std::cout << "UNpausing the simulation" << std::endl;
}
}

void Broker::*run_io_service*(boost::asio::io_service &io_service_*)* {
io_service_.run();
}

//this function is called when a message is received
void Broker::*messageReceiveCallback*(std::string message*)* {
boost::unique_lock<boost::mutex> lock(m_pause_mutex);
{
std::cout << "The message arrived in the handler" << std::endl;
m_universal+= 100; //remove the hardcoding later
Simulator::Schedule(MilliSeconds(m_universal), &Broker::pause, this); //
<---here is where the the problem occurs
std::cout << "Schedule done" << std::endl; <---never reaches here
m_cond_cnn.notify_all(); -->should cause the unpausing
}
}

*////////main function is :*
.....
std::cout << "declaring a broker" << std::endl;
sim_mob::Broker broker("localhost","6745");
std::cout << "declaring a broker_" << std::endl;
if(!broker.*start*())
{
std::cout << "Broker Start Failed" << std::endl;
}

Simulator::*Stop*(Seconds(10));
Simulator::*Run*();
Simulator::*Destroy* ();
std::cout << "Simulation Destroyed, joining the ASIO thread now" <<
std::endl;
......

*what I get in the console:*
declaring a broker
Connection::Connection()
In Broker::Broker()
declaring a broker_
Connected

{"DATA":[{"MESSA
......blah blah
Broker::start returning
pausing the simulation
ConnectionHandler::readHandler incoming''
The message arrived in the handler
assert failed. cond="SystemThread::Equals (m_main)",
msg="Simulator::Schedule Thread-unsafe invocation!",
file=../src/core/model/default-simulator-impl.cc, line=222
terminate called without an active exception


line 222 is refering to Schedule function :
NS_ASSERT_MSG (SystemThread::Equals (m_main), "Simulator::Schedule
Thread-unsafe invocation!");

I will highly appreciate if you kindly let me know what this is and how i
can solve it.
thank you


*
*
--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to ns-3-users-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/groups/opt_out.
vahid Saber
2013-07-10 06:11:27 UTC
Permalink
Ok, I guess nobody has any reply, so I am replying to myself:

the line 222 speaks for itself: apparently ns3 doesnt like to be scheduled
from any thread except the main thread. since the messagereceive callback
function is called from another thread, we cannot put Simulator::Schedule
in there. instead, I put it somewhere in the pause function (which is
called by the main thread) and the problem is solved!
Post by vahid Saber
Sorry if the question might be trivial, still can be interesting. Your
1. I am trying to pause and resume the simulation flow using the
messages received from outside ns3.
2. I want the ns3 pause until it receives information from an external
entity.
3. I have setup the infrastructure by creating a broker having the
*///////// BROKER*
//this is the first function and it uses boost::asio library to set up
//a connection and start reading(a thread is branched for this purpose)
bool Broker::*start()* {
m_universal = 0;
//connect and start reading
if(!m_cnn.connect(m_simmobility_address, m_simmobility_port, m_session))
{
return false;
}
//start a thread for io_service_run
m_io_service_runner = boost::thread(&Broker::run_io_service,
this, boost::ref(m_io_service));
std::cout << "Broker::start returning" << std::endl;
Simulator::ScheduleNow(&Broker::pause, this);
return true;
}
//I figured this would pause the simulation. I hope I was right.??
void Broker::*pause()*
{
boost::unique_lock<boost::mutex> lock(m_pause_mutex);
{
std::cout << "pausing the simulation" << std::endl;
m_cond_cnn.wait(lock);
std::cout << "UNpausing the simulation" << std::endl;
}
}
void Broker::*run_io_service*(boost::asio::io_service &io_service_*)* {
io_service_.run();
}
//this function is called when a message is received
void Broker::*messageReceiveCallback*(std::string message*)* {
boost::unique_lock<boost::mutex> lock(m_pause_mutex);
{
std::cout << "The message arrived in the handler" << std::endl;
m_universal+= 100; //remove the hardcoding later
Simulator::Schedule(MilliSeconds(m_universal), &Broker::pause, this); //
<---here is where the the problem occurs
std::cout << "Schedule done" << std::endl; <---never reaches here
m_cond_cnn.notify_all(); -->should cause the unpausing
}
}
*////////main function is :*
.....
std::cout << "declaring a broker" << std::endl;
sim_mob::Broker broker("localhost","6745");
std::cout << "declaring a broker_" << std::endl;
if(!broker.*start*())
{
std::cout << "Broker Start Failed" << std::endl;
}
Simulator::*Stop*(Seconds(10));
Simulator::*Run*();
Simulator::*Destroy* ();
std::cout << "Simulation Destroyed, joining the ASIO thread now" <<
std::endl;
......
*what I get in the console:*
declaring a broker
Connection::Connection()
In Broker::Broker()
declaring a broker_
Connected
{"DATA":[{"MESSA
......blah blah
Broker::start returning
pausing the simulation
ConnectionHandler::readHandler incoming''
The message arrived in the handler
assert failed. cond="SystemThread::Equals (m_main)",
msg="Simulator::Schedule Thread-unsafe invocation!",
file=../src/core/model/default-simulator-impl.cc, line=222
terminate called without an active exception
NS_ASSERT_MSG (SystemThread::Equals (m_main), "Simulator::Schedule
Thread-unsafe invocation!");
I will highly appreciate if you kindly let me know what this is and how i
can solve it.
thank you
*
*
--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to ns-3-users-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/groups/opt_out.
Loading...