-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.cpp
31 lines (27 loc) · 852 Bytes
/
writer.cpp
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
#include <ctime>
#include <fcntl.h>
#include <stdexcept>
#include <unistd.h>
#include "common.h"
#include "ring_buffer.h"
int main(int argc, char* argv[]) {
set_affinity(1);
if (argc != 2) {
throw std::runtime_error("Invalid arguments passed. Usage: ./writer <time-to-sleep>");
}
uint32_t const WRITER_TIMESTAMP_NSEC = atoi(argv[1]);
int fd;
ring_buffer* buffer_ptr = open_shared_memory(fd);
std::timespec to_write, to_sleep;
to_sleep.tv_sec = 0;
to_sleep.tv_nsec = WRITER_TIMESTAMP_NSEC;
for (int i = 0; i < OPERATIONS_CNT; ++i) {
clock_gettime(CLOCK_REALTIME, &to_write);
while (!buffer_ptr->write(to_write)) {
}
if (WRITER_TIMESTAMP_NSEC) {
clock_nanosleep(CLOCK_REALTIME, 0, &to_sleep, NULL);
}
}
close_shared_memory(fd, buffer_ptr);
}