Can boost spin_condition be used for process synchronization?

Viewed 34

I see below code in interprocess_condition, I know that interprocess_condition is used for process synchronization, but I am not sure whether spin_condition can be.

   private:
   #if defined(BOOST_INTERPROCESS_CONDITION_USE_POSIX)
      ipcdetail::posix_condition m_condition;
   #elif defined(BOOST_INTERPROCESS_CONDITION_USE_WINAPI)
      ipcdetail::winapi_condition m_condition;
   #else
      ipcdetail::spin_condition m_condition;   // this condition
   #endif

doubt

Because I found that the spin_condition is implemented through the volatile keyword variable. Can volatile keyword variables be used as shared memory between processes?

   spin_mutex  m_enter_mut;
   volatile boost::uint32_t    m_command;
   volatile boost::uint32_t    m_num_waiters;
1 Answers

It can. Memory is just that, memory.

If you share it between processes, it effectively makes threads from different processes behave like threads from the same process in relation to that memory location.

However, unless lock contention is very rare, a spin-lock without back-off is recipe for very inefficient power consumption and throughput. Alternatively, if efficiency is not your concern but you care about absolute latency, you will have to be in strict control of thread scheduling (how many threads coexist on the same machine/NUMA node/..., thread affinity, thread priority and real-time scheduling options to name a few). In practice, you won't have that control unless you already knew the answers to your question, so the recommendation must be to stay away from spin-lock-only synchronization.

Related