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
.wait
, most IDEs have a something likectrl + shift + f
to find something in all files, search for.wait(
and for all results you find make sure it has 2 arguments where the second one is a predicate.bool
to becometrue
, that's close to what other languages haveEvent
objects for, acondition_variable
is more low-level than that. it is used to build other primitives like theEvent