| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#include <iostream> |
| 2 |
|
|
#include <thread> |
| 3 |
|
|
#include "shared_memory/locked_condition_variable.hpp" |
| 4 |
|
|
#include "shared_memory/shared_memory.hpp" |
| 5 |
|
|
|
| 6 |
|
|
#include <signal.h> |
| 7 |
|
|
#include <stdlib.h> |
| 8 |
|
|
#include <time.h> |
| 9 |
|
|
#include <unistd.h> |
| 10 |
|
|
#include <iostream> |
| 11 |
|
|
|
| 12 |
|
|
static bool RUNNING = true; |
| 13 |
|
|
|
| 14 |
|
✗ |
void stop(int) |
| 15 |
|
|
{ |
| 16 |
|
✗ |
RUNNING = false; |
| 17 |
|
|
} |
| 18 |
|
|
|
| 19 |
|
✗ |
void execute() |
| 20 |
|
|
{ |
| 21 |
|
✗ |
shared_memory::LockedConditionVariable cv("main_memory", false); |
| 22 |
|
|
|
| 23 |
|
✗ |
cv.lock_scope(); |
| 24 |
|
|
|
| 25 |
|
|
// ping was waiting for pong to start, |
| 26 |
|
|
// informing ping it can go ahead |
| 27 |
|
✗ |
cv.notify_one(); |
| 28 |
|
|
|
| 29 |
|
✗ |
while (RUNNING) |
| 30 |
|
|
{ |
| 31 |
|
✗ |
cv.wait(); |
| 32 |
|
|
|
| 33 |
|
|
int count; |
| 34 |
|
✗ |
shared_memory::get<int>("main_memory", "count", count); |
| 35 |
|
✗ |
count++; |
| 36 |
|
✗ |
shared_memory::set<int>("main_memory", "count", count); |
| 37 |
|
|
|
| 38 |
|
✗ |
std::cout << "\t\tpong " << count << "\n"; |
| 39 |
|
|
|
| 40 |
|
✗ |
usleep(2000); |
| 41 |
|
|
|
| 42 |
|
✗ |
cv.notify_one(); |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
✗ |
cv.unlock_scope(); |
| 46 |
|
|
} |
| 47 |
|
|
|
| 48 |
|
✗ |
int main() |
| 49 |
|
|
{ |
| 50 |
|
|
// cleaning and exit on ctrl+c |
| 51 |
|
|
struct sigaction cleaning; |
| 52 |
|
✗ |
cleaning.sa_handler = stop; |
| 53 |
|
✗ |
sigemptyset(&cleaning.sa_mask); |
| 54 |
|
✗ |
cleaning.sa_flags = 0; |
| 55 |
|
✗ |
sigaction(SIGINT, &cleaning, nullptr); |
| 56 |
|
|
|
| 57 |
|
✗ |
execute(); |
| 58 |
|
|
} |
| 59 |
|
|
|