Leon
Leon

Reputation: 2077

Memory fence with std::system_clock::now()

I need to add two memory fences into my codes, in order to prevent my codes from being reordered by either the compiler or CPU.

Like this:

rec.time_stamp0 = std::system_clock::now();
std::atomic_signal_fence( std::memory_order::release );
/*
  do many things...
*/
std::atomic_signal_fence( std::memory_order::acquire );
rec.time_stamp1 = std::system_clock::now();

Here I want to ensure that rec.time_stamp0 = std::system_clock::now(); certainly be executed before all other codes, and that rec.time_stamp1 = std::system_clock::now(); must be after all others.

My questions:

  1. Does std::system_clock::now() natively come with a memory fence(it's a syscall)? So explicit fence is not necessary?
  2. Should I use std::atomic_thread_fence, or std::atomic_signal_fence here(My program is single thread)?

Upvotes: 0

Views: 59

Answers (0)

Related Questions