Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use this_thread::sleep_for on Windows #32

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/realtime_tools/realtime_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@

#include <boost/thread/mutex.hpp>

#ifdef _WIN32
#include <chrono>
#include <thread>
#endif

namespace realtime_tools
{

Expand Down Expand Up @@ -154,7 +159,13 @@ class RealtimeBuffer
mutex_.lock();
#else
while (!mutex_.try_lock())
{
#ifdef _WIN32
std::this_thread::sleep_for(std::chrono::microseconds(500));
#else
usleep(500);
#endif
}
#endif
}

Expand Down
31 changes: 27 additions & 4 deletions include/realtime_tools/realtime_publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
#include <boost/thread/thread.hpp>
#include <boost/thread/condition.hpp>

#ifdef _WIN32
#include <chrono>
#include <thread>
#endif

namespace realtime_tools {

template <class Msg>
Expand Down Expand Up @@ -78,7 +83,13 @@ class RealtimePublisher : boost::noncopyable
{
stop();
while (is_running())
{
#ifdef _WIN32
std::this_thread::sleep_for(std::chrono::microseconds(100));
#else
usleep(100);
#endif
}

publisher_.shutdown();
}
Expand Down Expand Up @@ -153,7 +164,13 @@ class RealtimePublisher : boost::noncopyable
#else
// never actually block on the lock
while (!msg_mutex_.try_lock())
{
#ifdef _WIN32
std::this_thread::sleep_for(std::chrono::microseconds(200));
#else
usleep(200);
#endif
}
#endif
}

Expand Down Expand Up @@ -190,11 +207,17 @@ class RealtimePublisher : boost::noncopyable
while (turn_ != NON_REALTIME && keep_running_)
{
#ifdef NON_POLLING
updated_cond_.wait(lock);
updated_cond_.wait(lock);
#else
unlock();
usleep(500);
lock();
unlock();

#ifdef _WIN32
std::this_thread::sleep_for(std::chrono::microseconds(500));
#else
usleep(500);
#endif

lock();
#endif
}
outgoing = msg_;
Expand Down
11 changes: 11 additions & 0 deletions src/realtime_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@

#include <realtime_tools/realtime_clock.h>

#ifdef _WIN32
#include <chrono>
#include <thread>
#endif

namespace realtime_tools
{

Expand Down Expand Up @@ -126,7 +131,13 @@ namespace realtime_tools
mutex_.lock();
#else
while (!mutex_.try_lock())
{
#ifdef _WIN32
std::this_thread::sleep_for(std::chrono::microseconds(500));
#else
usleep(500);
#endif
}
#endif
}

Expand Down