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

thread-safe implementation in realtime_publisher #50

Open
Maverobot opened this issue Feb 9, 2020 · 2 comments
Open

thread-safe implementation in realtime_publisher #50

Maverobot opened this issue Feb 9, 2020 · 2 comments

Comments

@Maverobot
Copy link

Maverobot commented Feb 9, 2020

When reading the code, I have a few questions regarding realtime_publisher.h

  1. volatile bool is used for cross-threads flags. As far as I know, it is undefined behavior in C++. Should we replace that with std::atomic<bool>?
    Some related discussions:
  2. int turn_ is also being used in both threads and not explicitly guarded by any mutex.
  3. Why is NO_POLLING not activated by default? Does it mean the current release version is actually using polling instead of condition_variable, which is in my opinion a much better choice?
@Maverobot Maverobot changed the title volatile bool -> atomic<bool> thread-safe implementation in realtime_publisher Feb 9, 2020
@matthew-reynolds
Copy link
Member

  1. Indeed, volatile bool here is incorrect and dangerous. Looks like something written nearly 10 years ago that's been carried forward ever since. I don't think the solution is atomic<bool> though, I think atomic_flag is more suited since it is guaranteed to be implemented lock-free, while the atomic<T> types may be implemented with locks. (Practically, I imagine all implementation for an atomic<bool> are lock-free, but we should still strive for correctness.)
    Looks like Use std::atomic instead of volatile #51 was just opened to address this issue - Let's move this discussion there?

  2. Agreed, int turn_ also looks like a good candidate for an std::atomic<> type, this time std::atomic<int>.

  3. I don't know why NON_POLLING isn't default (@bmagyar thoughts? My only guess is that when it was written in 2013, it required boost and was therefore "heavier"?), but from Fix building realtime_publisher using NON_POLLING #43 it looks like it currently doesn't build anyways. Perhaps once it's fixed we should investigate enabling it by default, at least in Noetic.

@bmagyar
Copy link
Member

bmagyar commented Mar 11, 2020

Thank you guys for chasing this.

I propose the following:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants