concurrentqueue icon indicating copy to clipboard operation
concurrentqueue copied to clipboard

BlockingConcurrentQueue::wait_dequeue_timed can't quit

Open HowToExpect opened this issue 2 years ago • 10 comments

Under Linux, there will be a time synchronization operation. The current time is March 12, 2022. If set to March 1, 2022, wait_time will not return.

HowToExpect avatar Mar 12 '22 02:03 HowToExpect

Not sure I understand. Can you provide an example?

cameron314 avatar Mar 12 '22 02:03 cameron314

`#include #include "blockingconcurrentqueue.h"

using namespace std; using namespace moodycamel;

void timeCallback() { system("date -s 2022-03-01"); }

int main() {

BlockingConcurrentQueue<int> queue;

// current time 2022-03-21
int value = 0;
queue.wait_dequeue_timed(value, 60 * 1000 * 1000);

//have function call timeCallback();
cout << "hello world" << endl;

return 0;

} `

hello world can't output

HowToExpect avatar Mar 12 '22 02:03 HowToExpect

I see! This is because POSIX semaphores use absolute deadlines based on CLOCK_REALTIME. I'll see if I can conditionally detect the presence of a glibc 2.30 extension that would work with CLOCK_MONOTONIC instead.

cameron314 avatar Mar 12 '22 03:03 cameron314

I went to check the relevant information and it really has something to do with CLOCK_REALTIME

HowToExpect avatar Mar 12 '22 04:03 HowToExpect

Yes. Glibc 2.30+ has a non-standard sem_clockwait function that allows CLOCK_MONOTONIC time to be specified instead.

cameron314 avatar Mar 12 '22 04:03 cameron314

Should be fixed if you have glibc 2.30+ available.

cameron314 avatar Mar 12 '22 13:03 cameron314

I checked that my glibc is 2.25, so sem_clockwait is not supported, so only pthread_mutex_t and pthread_cond_t can be used to implement semaphores

HowToExpect avatar Mar 14 '22 06:03 HowToExpect

pthread_cond_timedwait() with CLOCK_MONOTONIC is supported

HowToExpect avatar Mar 14 '22 06:03 HowToExpect

pthread_mutex_t and pthread_cond_t are semaphores that I see in poco

HowToExpect avatar Mar 14 '22 06:03 HowToExpect

pthread_cond_t is a condition variable. It can be used to implement a semaphore but it's not as efficient. I'll see if I can add an optional alternative implementation that uses it though.

cameron314 avatar Mar 14 '22 11:03 cameron314