forked from mxe/mxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boost-test.cpp
60 lines (52 loc) · 1.65 KB
/
boost-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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* This file is part of MXE. See LICENSE.md for licensing information.
*/
#include <iostream>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/tss.hpp>
boost::thread_specific_ptr<int> ptr;
// https://www.boost.org/doc/libs/1_76_0/libs/context/doc/html/context/context.html
#include <boost/context/fiber_fcontext.hpp>
namespace ctx=boost::context;
void test_thread()
{
if (ptr.get() == 0) {
ptr.reset(new int(0));
}
std::cout << "Hello, World! from thread" << std::endl;
}
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
boost::archive::xml_oarchive oa(std::cout);
std::string s = "\n\n Hello, World!\n\n";
oa << BOOST_SERIALIZATION_NVP(s);
boost::thread thrd(test_thread);
thrd.join();
int data=0;
ctx::fiber f1{[&data](ctx::fiber&& f2) {
std::cout << "f1: entered first time: " << data << std::endl;
data+=1;
f2=std::move(f2).resume();
std::cout << "f1: entered second time: " << data << std::endl;
data+=1;
f2=std::move(f2).resume();
std::cout << "f1: entered third time: " << data << std::endl;
return std::move(f2);
}};
f1=std::move(f1).resume();
std::cout << "f1: returned first time: " << data << std::endl;
data+=1;
f1=std::move(f1).resume();
std::cout << "f1: returned second time: " << data << std::endl;
data+=1;
f1=std::move(f1).resume_with([&data](ctx::fiber&& f2){
std::cout << "f2: entered: " << data << std::endl;
data=-1;
return std::move(f2);
});
std::cout << "f1: returned third time" << std::endl;
return 0;
}