Skip to content

Commit ae70e4d

Browse files
author
wintel2014
committed
2018_03_16
1 parent d10c7a5 commit ae70e4d

File tree

142 files changed

+52336
-106
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+52336
-106
lines changed

C++/Boost/Coroutine.tar

62.9 MB
Binary file not shown.

C++/Boost/Coroutine/Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
LIB_DIR=/home/jz/boost_install_dir/lib/
22
INCLUDE=-I/home/jz/boost_install_dir/include/
33

4-
all:same_fringe fibonacci callcc task
4+
all:same_fringe fibonacci callcc task_object
55

66
same_fringe: same_fringe.cpp
77
g++ $^ -std=c++14 $(INCLUDE) /home/jz/boost_install_dir/lib/libboost_context.a /home/jz/boost_install_dir/lib/libboost_thread.a -g3 -o $@
@@ -14,5 +14,8 @@ fibonacci: fibonacci.cpp
1414
callcc:callcc.cpp
1515
g++ $^ -std=c++14 $(INCLUDE) /home/jz/boost_install_dir/lib/libboost_context.a /home/jz/boost_install_dir/lib/libboost_thread.a -g3 -o $@
1616

17-
task:task.cpp
18-
g++ $^ -std=c++14 $(INCLUDE) /home/jz/boost_install_dir/lib/libboost_context.a /home/jz/boost_install_dir/lib/libboost_thread.a -g3 -o $@
17+
task_object:task_object.cpp
18+
g++ $^ -std=c++14 $(INCLUDE) /home/jz/boost_install_dir/lib/libboost_context.a /home/jz/boost_install_dir/lib/libboost_thread.a -g3 -o $@ -lpthread
19+
20+
clean:
21+
find . -name '*' -executable | egrep -v '\.$$' |xargs rm

C++/Boost/Coroutine/callcc

403 KB
Binary file not shown.

C++/Boost/Coroutine/callcc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int main() {
1919
[&a](ctx::continuation && sink){
2020
a=0;
2121
long int b=1;
22-
std::cout<<"main<lambda>\n";
22+
std::cout<<"main::<lambda>\n";
2323
for(;;){
2424
sink=sink.resume();
2525
std::cout <<"callcc: "<< a << "\n";

C++/Boost/Coroutine/fibonacci

440 KB
Binary file not shown.

C++/Boost/Coroutine/same_fringe

569 KB
Binary file not shown.

C++/Boost/Coroutine/task_object

568 KB
Binary file not shown.

C++/Boost/Coroutine/task_object.cpp

Lines changed: 0 additions & 92 deletions
This file was deleted.

C++/Boost/Coroutine/task_object.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
task_object_multi_thread.cpp
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <cstdlib>
2+
#include <iostream>
3+
#include <string>
4+
#include <boost/coroutine2/all.hpp>
5+
6+
template <typename T>
7+
using coroutine = boost::coroutines2::coroutine<T>;
8+
namespace ctx=boost::context;
9+
10+
class Task {
11+
public:
12+
Task(std::string);
13+
~Task() {mRun=false;}
14+
void Process()
15+
{
16+
std::cout<<"Task::Process\n";
17+
}
18+
19+
void Suspend()
20+
{
21+
if(mFromContext)
22+
mFromContext = mFromContext.resume();
23+
}
24+
void Resume()
25+
{
26+
if(mTaskContext)
27+
mTaskContext = mTaskContext.resume();
28+
}
29+
30+
void Stop()
31+
{
32+
mRun = false;
33+
}
34+
private:
35+
bool mRun;
36+
std::string mName;
37+
boost::context::continuation mTaskContext;
38+
boost::context::continuation mFromContext;
39+
40+
};
41+
42+
43+
Task::Task(std::string name):
44+
mRun(true),
45+
mName(name),
46+
mTaskContext(
47+
ctx::callcc( [this](ctx::continuation && from)->ctx::continuation &&
48+
{
49+
mFromContext = from.resume();
50+
while(mRun)
51+
{
52+
std::cout<<"Enter Task-"<<mName<<std::endl;
53+
this->Process();
54+
sleep(1);
55+
std::cout<<"Exit Task-"<<mName<<std::endl;
56+
//mFromContext = mFromContext.resume();
57+
}
58+
59+
std::cout<<"Exit Task-"<<mName<<std::endl;
60+
return std::move(mFromContext);
61+
}
62+
)
63+
)
64+
{
65+
}
66+
67+
68+
int main()
69+
{
70+
Task T1("1");
71+
Task T2("2");
72+
Task T3("3");
73+
Task T4("4");
74+
Task T5("5");
75+
76+
size_t loop = 0;
77+
while(loop++ < 20)
78+
{
79+
std::cout<<"=================Main::Loop"<<loop<<"================="<<std::endl;
80+
T1.Resume();
81+
/*
82+
T2.Resume();
83+
84+
if(loop == 5)
85+
{
86+
std::cout<<"T1 stop\n";
87+
T1.Stop();
88+
}
89+
T3.Resume();
90+
if(loop == 10)
91+
{
92+
std::cout<<"T2 stop\n";
93+
T2.Stop();
94+
T4.Resume();
95+
T5.Resume();
96+
}
97+
*/
98+
99+
}
100+
}
101+
102+

C++/Boost/Coroutine/task_object_multi_thread.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ class Task {
3636
}
3737
void Construct(DEventNode *pNode)
3838
{
39-
mTaskContext = ctx::callcc( [this, pNode](ctx::continuation && from)->ctx::continuation &&
39+
mTaskContext = ctx::callcc( [this, pNode](ctx::continuation && from)
4040
{
4141
mFromContext = from.resume();
4242
while(mRun)
4343
{
4444
std::cout<<"Enter Task-"<<mName<<std::endl;
4545
pNode->Process();
46-
std::cout<<"Exit Task-"<<mName<<std::endl;
4746
sleep(1);
47+
std::cout<<"Exit Task-"<<mName<<std::endl;
4848
BackToMainContext();
4949
}
5050

51-
std::cout<<"Exit Task-"<<mName<<std::endl;
51+
std::cout<<"!!!!!!!!!!!Task-"<<mName<<std::endl;
5252
return std::move(mFromContext);
5353
}
5454
);
@@ -59,7 +59,7 @@ class Task {
5959
if(mFromContext)
6060
mFromContext = mFromContext.resume();
6161
else
62-
std::cout<<"FromContext is null!!!!!!\n";
62+
std::cout<<"MainContext is null!!!!!!\n";
6363
}
6464
void Resume()
6565
{
@@ -105,14 +105,27 @@ int main()
105105
Task* pT3 = &v[2];
106106

107107
pT1->Construct(&Node);
108+
109+
std::thread T1(
110+
[pT1]()
111+
{pT1->BackToMainContext();}
112+
);
108113

109114

110115
size_t loop = 0;
111116
while(loop++ < 10)
112117
{
113-
std::cout<<"Thread-"<<gettid()<<"=================1 Main::Loop"<<loop<<"================="<<std::endl;
118+
if(loop == 9)
119+
{
120+
pT1->Stop();
121+
std::cout<<"Stop T1\n";
122+
}
123+
124+
std::cout<<"Thread-"<<gettid()<<"=================A Main::Loop"<<loop<<"================="<<std::endl;
114125
pT1->Resume();
115-
std::cout<<"Thread-"<<gettid()<<"=================2 Main::Loop"<<loop<<"================="<<"\n\n";
126+
std::cout<<"Thread-"<<gettid()<<"=================B Main::Loop"<<loop<<"================="<<"\n\n";
116127
}
128+
129+
T1.join();
117130
}
118131

C++/Boost/LockFree/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spsc_consume_all:spsc_consume_all.cpp
2+
g++ $^ -std=c++14 -g3 -lpthread -lboost_system -lboost_thread -o $@
3+

C++/Boost/LockFree/spsc.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <iostream>
2+
#include <time.h>
3+
#include <thread>
4+
#include <list>
5+
6+
#include <boost/lockfree/spsc_queue.hpp>
7+
#include <boost/thread/mutex.hpp>
8+
#include <boost/thread/condition_variable.hpp>
9+
#include <boost/circular_buffer.hpp>
10+
11+
12+
13+
#define LOOP_COUNT 10000000
14+
#define QUEUE_CAPACITY 10000000
15+
16+
17+
boost::lockfree::spsc_queue<int, boost::lockfree::capacity<QUEUE_CAPACITY>> spscq;
18+
19+
int count = 0;
20+
21+
void nonblocking_productor()
22+
{
23+
for (int i = 0; i < LOOP_COUNT; i++)
24+
{
25+
spscq.push(i);
26+
}
27+
}
28+
29+
void nonblocking_customer()
30+
{
31+
int value = 0;
32+
while (1)
33+
{
34+
if (spscq.pop(value))
35+
{
36+
++count;
37+
}
38+
else
39+
{
40+
sleep(1);
41+
}
42+
43+
if (count >= LOOP_COUNT)
44+
{
45+
break;
46+
}
47+
}
48+
}
49+
50+
51+
void test_nonblocking()
52+
{
53+
clock_t start = clock();
54+
55+
std::thread *nc_t = new std::thread((&nonblocking_customer));
56+
std::thread *np_t = new std::thread((&nonblocking_productor));
57+
nc_t->join();
58+
np_t->join();
59+
60+
clock_t end = clock();
61+
printf("[test_nonblocking]\nQUEUE_CAPACITY:%d\ncost:%dms\n", QUEUE_CAPACITY, end - start);
62+
printf("count:%d\n", count);
63+
64+
delete nc_t;
65+
delete np_t;
66+
}
67+
68+
int main()
69+
{
70+
test_nonblocking();
71+
}

C++/Boost/LockFree/spsc_consume_all

1.3 MB
Binary file not shown.

0 commit comments

Comments
 (0)