-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (46 loc) · 1018 Bytes
/
main.cpp
File metadata and controls
66 lines (46 loc) · 1018 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "PythonInterpreter.h"
#include <fmt/color.h>
#include <fmt/printf.h>
#include <atomic>
#include <thread>
std::atomic<bool> g_stop = false;
void loop(int incr)
{
std::string code = fmt::format("i=i+{}\nprint(f\"i={{i}}\")\n", incr);
PythonInterpreter py;
py.runSimpleString("i=0");
while (!g_stop)
{
py.runSimpleString(code.c_str());
}
}
int main()
{
PythonInterpreter::initialize();
PythonInterpreter py1;
const char* c1 = R"(i=12
print(f"-------- i= {i}")
)";
py1.runSimpleString(c1);
PythonInterpreter py2;
const char* c2 = R"(i=1
print(f"2-------- i= {i}")
)";
const char* c3 = R"(i=i+1
print(f"3-------- i= {i}")
)";
py2.runSimpleString(c2);
py1.runSimpleString(c3);
py2.runSimpleString(c3);
std::thread t1(loop, 1);
std::thread t2(loop, -2);
const time_t endTime = time(0) + 5;
while (time(0) < endTime)
{
sleep(1);
}
g_stop = true;
t1.join();
t2.join();
return 0;
}