Reputation: 41
There is such an interesting little code (I'll take the most important thing, since my project is quite large
void RunForever(int time_out)
{
int criticalSize = 3;
size_t size_sheldure = PopSheldure();
while (isRunning)
{
if (size_sheldure < criticalSize)
UpdateScheldure(0);
auto lineScheldure = (*(m_scheldure)).front();
m_radioConnector.UpdateParametersRadio(lineScheldure);
ptime near_future = from_iso_string(lineScheldure.GetUTCTime());
ptime current_time = ComputeCurrentLocalTime();
std::cout << ((near_future - current_time).total_seconds()) << std::endl;
if ((near_future - current_time).total_seconds() < 0)
{
PopSheldure();
continue;
}
m_timer.expires_after(boost::asio::chrono::seconds(time_out));
m_timer.async_wait([this](const boost::system::error_code& ec)
{
if (!ec)
{
std::lock_guard<std::mutex> lock(mtx);
isTimerFinish = true;
}
cv_isListen.notify_one();
std::cout << "Timer finished" << std::endl;
});
if (lineScheldure.GetStatusInfo() == BasicDefinition::RadioStatus::Listen)
StartListenSession(time_out);
if (lineScheldure.GetStatusInfo() == BasicDefinition::RadioStatus::Send)
StartSendSession(time_out);
size_sheldure = PopSheldure();
}
}
There are also two functions that are called depending on the current status:
void StartSendSession(int time_out)
{
auto sendHandler = std::bind(&ModemConnector::SendHandler, this, std::placeholders::_1);
if (m_modem)
{
}
std::unique_lock<std::mutex> lock(mtx);
cv_isSend.wait(lock);
isTimerFinish = false;
std::cout << "Send --> Listen" << std::endl;
}
void StartListenSession(int time_out)
{
if (m_modem)
{
}
std::unique_lock<std::mutex> lock(mtx);
std::cout << "Check" << std::endl;
cv_isListen.wait(lock);
isTimerFinish = false;
std::cout << "Listen --> Send" << std::endl;
}
The problem is that sending and receiving is done on a timer. When the timer ends, I need the thread to continue executing, but in reality, usually starting from the second timer, one of the functions gets stuck. What is the reason for this behavior of functions?
output
1
Check
Timer finished
Listen --> Send
2
Timer finished
Upvotes: 1
Views: 43
Reputation: 393664
usually starting from the second timer, one of the functions gets stuck. What is the reason for this behavior of functions?
From your code it looks like the same timer is being re-used always. However, this
m_timer.expires_after(boost::asio::chrono::seconds(time_out));
cancels any timer still pending. In your completion handler, you only check if (!ec)
which means that on a canceled timer you do NOT set isTimerFinish
.
Something tells me you can greatly simplify this design, but it's not possible to make concrete suggestions without the rest of the code.
Upvotes: 0