on-internet
← Community

Can C++ "happens-before" order contradict physical wall-clock execution time?

Stack Overflow ·

Consider the following C++ example: #include <chrono> #include <ctime> #include <iostream> #include <mutex> #include <string> #include <thread> int stock = 10; std::mutex mtx; time_t time_buy = 0; time_t time_clear = 0; std::chrono::time_point<std::chrono::steady_clock> t_buy_start; std::string clear_stock() { std::unique_lock lock(mtx); // #1 time_clear = time(NULL); stock = 0; return "clear"; } std::string buy_stock() { std::unique_lock lock(mtx); // #2 time_buy = time(NULL); if (stock >= 1) { stock--; return "ok"; } return "empty"; } int main() { std::string res_clear; std::string res_buy;

Comments

Sign in or sign up to comment and upvote.

No comments yet.