Skip to content

Commit f07b25d

Browse files
committed
fix(eventx):1.10.2, 修复了ThreadPool的bug,以及http示例的问题
1. 解决TimerPool::doAfter(),在定时完成之后,没有立即析构定时器的问题 2. 修复了examples/http/server/async_respond 中访问,没有返回的问题
1 parent c6fcd29 commit f07b25d

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

examples/http/server/async_respond/async_respond.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
* project authors may be found in the CONTRIBUTORS.md file in the root
1818
* of the source tree.
1919
*/
20+
#include <thread>
21+
2022
#include <tbox/base/log.h>
2123
#include <tbox/base/log_output.h>
2224
#include <tbox/base/scope_exit.hpp>
2325
#include <tbox/log/async_stdout_sink.h>
2426
#include <tbox/event/signal_event.h>
2527
#include <tbox/eventx/timer_pool.h>
28+
#include <tbox/eventx/thread_pool.h>
2629
#include <tbox/http/server/server.h>
2730

2831
using namespace tbox;
@@ -46,7 +49,10 @@ int main(int argc, char **argv)
4649
auto sp_loop = Loop::New();
4750
auto sp_sig_event = sp_loop->newSignalEvent();
4851

49-
TimerPool timers(sp_loop);
52+
TimerPool timer_pool(sp_loop);
53+
ThreadPool thread_pool(sp_loop);
54+
55+
thread_pool.initialize(1);
5056

5157
SetScopeExitAction(
5258
[=] {
@@ -77,18 +83,39 @@ R"(
7783
<head>
7884
</head>
7985
<body>
80-
<p> <a href="/1" target="_blank">delay</a> </p>
81-
<p> <a href="/2" target="_blank">now</a> </p>
86+
<p> <a href="/1" target="_blank">nodelay</a> </p>
87+
<p> <a href="/2" target="_blank">delay 5 sec</a> </p>
88+
<p> <a href="/3" target="_blank">delay 0~10 sec</a> </p>
8289
</body>
8390
)";
8491
} else if (ctx->req().url.path == "/1") {
85-
timers.doAfter(std::chrono::seconds(10), [ctx] {
92+
ctx->res().status_code = StatusCode::k200_OK;
93+
ctx->res().body = ctx->req().url.path;
94+
95+
} else if (ctx->req().url.path == "/2") {
96+
timer_pool.doAfter(std::chrono::seconds(5), [ctx] {
8697
ctx->res().status_code = StatusCode::k200_OK;
8798
ctx->res().body = ctx->req().url.path;
8899
});
89-
} else if (ctx->req().url.path == "/2") {
90-
ctx->res().status_code = StatusCode::k200_OK;
91-
ctx->res().body = ctx->req().url.path;
100+
101+
} else if (ctx->req().url.path == "/3") {
102+
struct Tmp {
103+
int result;
104+
};
105+
auto tmp = std::make_shared<Tmp>();
106+
thread_pool.execute(
107+
[tmp] {
108+
//! 模拟随机的,不确定时长的阻塞性行为
109+
auto wait_msec = ::rand() % 10000;
110+
LogTrace("wait_msec: %d", wait_msec);
111+
std::this_thread::sleep_for(std::chrono::milliseconds(wait_msec));
112+
tmp->result = wait_msec;
113+
},
114+
[ctx, tmp] {
115+
ctx->res().status_code = StatusCode::k200_OK;
116+
ctx->res().body = "wait: " + std::to_string(tmp->result) + " msec";
117+
}
118+
);
92119
}
93120
}
94121
);
@@ -105,6 +132,9 @@ R"(
105132
LogInfo("stop");
106133
srv.cleanup();
107134

135+
thread_pool.cleanup();
136+
timer_pool.cleanup();
137+
108138
LogInfo("exit");
109139
return 0;
110140
}

modules/eventx/timer_pool.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ TimerPool::TimerToken TimerPool::Impl::doEvery(const Milliseconds &m_sec, Callba
5858
return TimerToken();
5959
}
6060

61-
auto new_timer = wp_loop_->newTimerEvent("TimerPool::doEvery");
62-
auto new_token = timers_.alloc(new_timer);
63-
new_timer->initialize(m_sec, event::Event::Mode::kPersist);
64-
new_timer->setCallback(std::move(cb));
65-
new_timer->enable();
61+
auto this_timer = wp_loop_->newTimerEvent("TimerPool::doEvery");
62+
auto new_token = timers_.alloc(this_timer);
63+
this_timer->initialize(m_sec, event::Event::Mode::kPersist);
64+
this_timer->setCallback(std::move(cb));
65+
this_timer->enable();
6666
return new_token;
6767
}
6868

@@ -73,11 +73,24 @@ TimerPool::TimerToken TimerPool::Impl::doAfter(const Milliseconds &m_sec, Callba
7373
return TimerToken();
7474
}
7575

76-
auto new_timer = wp_loop_->newTimerEvent("TimerPool::doAfter");
77-
auto new_token = timers_.alloc(new_timer);
78-
new_timer->initialize(m_sec, event::Event::Mode::kOneshot);
79-
new_timer->setCallback(std::move(cb));
80-
new_timer->enable();
76+
auto this_timer = wp_loop_->newTimerEvent("TimerPool::doAfter");
77+
auto new_token = timers_.alloc(this_timer);
78+
this_timer->initialize(m_sec, event::Event::Mode::kOneshot);
79+
80+
#if __cplusplus >= 201402L
81+
this_timer->setCallback([loop = wp_loop_, moved_cb = std::move(cb), this_timer] {
82+
moved_cb();
83+
loop->runNext([this_timer] { delete this_timer; });
84+
});
85+
#elif __cplusplus >= 201103L
86+
auto loop = wp_loop_;
87+
this_timer->setCallback([loop, cb, this_timer] {
88+
cb();
89+
loop->runNext([this_timer] { delete this_timer; });
90+
});
91+
#endif
92+
93+
this_timer->enable();
8194
return new_token;
8295
}
8396

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 10
24-
TBOX_VERSION_REVISION := 1
24+
TBOX_VERSION_REVISION := 2

0 commit comments

Comments
 (0)