c++ - Two threads accessing same variables, but one thread rarely happens. Non-atomics? -
i have 2 threads, t1 , t2, access several variables (ints , doubles), lets call them a, b , c. t1 on critical path , increments/decrements these variables via expensive compare-and-exchange.
std::atomic<double> a; std::atomic<double> b; std::atomic<double> c; std::atomic<uint32_t> d; . . . // extremely-common, critical path thread t1 d++; while(!a.compare_and_exchange(expectedval, newval); while(!b.compare_and_exchange(expectedval, newval); while(!c.compare_and_exchange(expectedval, newval); t2 occurs when does, adds variables mentioned above together.
// rare thread, t2 return + b - c; at moment use atomics. is there way can declare variables non-atomic, because 99.999% of time incremented same thread , on "rare thread" use memory barrier ensure cannot return a + b - c until "critical path" thread has finished writing stores?
this allow me add latency on occasional situation rare thread executes.
i'm not sure question if possible, might able refactor "communication" between threads share less state.
e.g., suppose "communicate" via a + b - c, in sense t1 updates components individually, , t2 reads particular combination. in case, use fewer atomic ops defining a, b, , c regular variables, , new variable, combination, atomic variable. t1 increment stuff normally, , use single atomic op update combination.
Comments
Post a Comment