-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshm_test.cpp
40 lines (29 loc) · 1.08 KB
/
shm_test.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
32
33
34
35
36
37
38
39
40
#include "interprocess.h"
// Demonstration of inter-script communication between C++ and Python
int main() {
SharedMemory* shm = new SharedMemory("force", 4096, true);
if (shm->fail()) {
delete shm;
return 1;
}
shm->write("shm created");
// on hold until the python side accesses this shared memory
PROCESS::wait(shm, "accessed");
std::cout << "CONNECTION WITH PYTHON SIDE ESTABLISHED. DO INPUT A MESSAGE (HIT 'ENTER' KEY TO QUIT):" << std::endl;
// sending input message continuously to shared memory
std::string text;
std::getline(std::cin, text);
while (! text.empty()) {
shm->write(text);
std::getline(std::cin, text);
}
// Notify python side for termination
shm->write("terminate");
// Wait till python side acknowledge the termination
PROCESS::wait(shm, "affirmative");
// Sleep for 1 second to allow proper memory clean up at Python's side
// requires #include <unistd.h> - included in interprocess.h
sleep(1);
delete shm;
return 0;
}