0

I've stumbled about this thread: Why is volatile not considered useful in multithreaded C or C++ programming?

and stumbled upon the following in the top voted answer...

However, memory barriers also ensure that all pending reads/writes are executed when the barrier is reached, so it effectively gives us everything we need by itself, making volatile unnecessary. We can just remove the volatile qualifier entirely.

Since C++11, atomic variables (std::atomic) give us all of the relevant guarantees.

I'm working on C++98 capable platform, so what memory barrier was available for C++98? I've tried to use mutex for mbed, but I can't logically determine if a mutex is a sufficient way to protect, for example, both a serial write and read happening in two simultaneous threads, since I don't have enough confidence in regards to thread-safety.

What is an easy way to access a simple shared resource in c++98?

Community
  • 1
  • 1
Tryb Ghost
  • 419
  • 1
  • 4
  • 14
  • You need a platform specific answer; please add your platform/OS to your question. – Richard Critten Jun 10 '19 at 11:15
  • @RichardCritten I'm working on mbed, so a microcontroller. To be more specific, LPC1768. – Tryb Ghost Jun 10 '19 at 11:24
  • Even with sufficient barriers, there are stuff in C++ that only be done with compiler support, such as dynamic initialization of function static variables on first invocation of the function. – curiousguy Jun 11 '19 at 00:46

2 Answers2

2

The C++98 Standard is single threaded (threads do not exist) so none in the standard. You will however have OS/Platform specific memory barriers.

Richard Critten
  • 2,138
  • 3
  • 13
  • 16
1

but I can't logically determine if a mutex is a sufficient way to protect, for example, both a serial write and read happening in two simultaneous threads

If the mutex code cannot guarantee that, they it's by definition broken. We would have to see that code to check whether it was properly done.

curiousguy
  • 8,038
  • 2
  • 40
  • 58