From 683c8eef15cff0546c58e99fe1531d88f3164dcd Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Fri, 8 May 2020 09:52:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/mk_thread.h | 16 ++++++++++++++++ api/source/mk_thread.cpp | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/api/include/mk_thread.h b/api/include/mk_thread.h index 87c651d1..d37f04c8 100644 --- a/api/include/mk_thread.h +++ b/api/include/mk_thread.h @@ -36,6 +36,22 @@ API_EXPORT mk_thread API_CALL mk_thread_from_tcp_session(mk_tcp_session ctx); */ API_EXPORT mk_thread API_CALL mk_thread_from_tcp_client(mk_tcp_client ctx); +/** + * 根据负载均衡算法,从事件线程池中随机获取一个事件线程 + * 如果在事件线程内执行此函数将返回本事件线程 + * 事件线程指的是定时器、网络io事件线程 + * @return 事件线程 + */ +API_EXPORT mk_thread API_CALL mk_thread_from_pool(); + +/** + * 根据负载均衡算法,从后台线程池中随机获取一个线程 + * 后台线程本质与事件线程相同,只是优先级更低,同时可以执行短时间的阻塞任务 + * ZLMediaKit中后台线程用于dns解析、mp4点播时的文件解复用 + * @return 后台线程 + */ +API_EXPORT mk_thread API_CALL mk_thread_from_pool_work(); + ///////////////////////////////////////////线程切换///////////////////////////////////////////// typedef void (API_CALL *on_mk_async)(void *user_data); diff --git a/api/source/mk_thread.cpp b/api/source/mk_thread.cpp index b73594e1..08778287 100644 --- a/api/source/mk_thread.cpp +++ b/api/source/mk_thread.cpp @@ -12,6 +12,7 @@ #include "mk_tcp_private.h" #include "Util/logger.h" #include "Poller/EventPoller.h" +#include "Thread/WorkThreadPool.h" using namespace std; using namespace toolkit; @@ -27,6 +28,14 @@ API_EXPORT mk_thread API_CALL mk_thread_from_tcp_client(mk_tcp_client ctx){ return (*client)->getPoller().get(); } +API_EXPORT mk_thread API_CALL mk_thread_from_pool(){ + return EventPollerPool::Instance().getPoller().get(); +} + +API_EXPORT mk_thread API_CALL mk_thread_from_pool_work(){ + return WorkThreadPool::Instance().getPoller().get(); +} + API_EXPORT void API_CALL mk_async_do(mk_thread ctx,on_mk_async cb, void *user_data){ assert(ctx && cb); EventPoller *poller = (EventPoller *)ctx;