How to make pthread_cond_timedwait() robust against system clock manipulations?

前端 未结 2 2029
渐次进展
渐次进展 2021-02-05 12:39

Consider the following source code, which is fully POSIX compliant:

#include 
#include 
#include 
#include 

        
2条回答
  •  花落未央
    2021-02-05 13:06

    You can defend your code against this problem. One easy way is to have one thread whose sole purpose is to watch the system clock. You keep a global linked list of condition variables, and if the clock watcher thread sees a system clock jump, it broadcasts every condition variable on the list. Then, you simply wrap pthread_cond_init and pthread_cond_destroy with code that adds/removes the condition variable to/from the global linked list. Protect the linked list with a mutex.

提交回复
热议问题