-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCondition.h
More file actions
38 lines (32 loc) · 988 Bytes
/
Condition.h
File metadata and controls
38 lines (32 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// Created by parallels on 2020/1/21.
//
#ifndef WEB_SERVER1_1_CONDITION_H
#define WEB_SERVER1_1_CONDITION_H
#include <errno.h>
#include <pthread.h>
#include <pthread.h>
#include <time.h>
#include <cstdint>
#include "MutexLock.h"
class Condition {
public:
explicit Condition(MutexLock &_mutex) : mutex(_mutex) {
pthread_cond_init(&cond, NULL);
}
~Condition() { pthread_cond_destroy(&cond); }
void wait() { pthread_cond_wait(&cond, mutex.get());
printf("wait\n"); fflush(stdout);}
int notify() { return pthread_cond_signal(&cond); }
void notifyAll() { pthread_cond_broadcast(&cond); }
bool waitForSeconds(int seconds) {
struct timespec abstime;
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_sec += static_cast<time_t>(seconds);
return ETIMEDOUT == pthread_cond_timedwait(&cond, mutex.get(), &abstime);
}
private:
MutexLock &mutex;
pthread_cond_t cond;
};
#endif //WEB_SERVER1_1_CONDITION_H