From 99541155081d4a29fa54ef98393b11c10fe658ae Mon Sep 17 00:00:00 2001 From: SisMaker <156736github> Date: Sun, 3 Apr 2022 17:32:48 +0800 Subject: [PATCH] =?UTF-8?q?ft=EF=BC=9A=20=E5=A4=9A=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E8=80=85=E7=9A=84=E5=85=A8=E5=B1=80=E5=AE=9A=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/gTimer.hrl | 5 ++++- src/gTimer.erl | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/gTimer.hrl b/include/gTimer.hrl index 894a1d9..8836c90 100644 --- a/include/gTimer.hrl +++ b/include/gTimer.hrl @@ -2,4 +2,7 @@ -define(gTimerCfg, gTimerCfg). %% 工作者数量 --define(workCnt, workCnt). \ No newline at end of file +-define(workCnt, workCnt). + +%% 三元表达式 +-define(IIF(Cond, Ret1, Ret2), (case Cond of true -> Ret1; _ -> Ret2 end)). \ No newline at end of file diff --git a/src/gTimer.erl b/src/gTimer.erl index 003af68..4a4ab3f 100644 --- a/src/gTimer.erl +++ b/src/gTimer.erl @@ -7,6 +7,7 @@ , stop/0 , startWork/1 , setTimer/2 + , setTimer/3 , getTimer/1 , delTimer/1 ]). @@ -37,14 +38,19 @@ stop() -> setTimer(Time, Msg) -> Cnt = ?gTimerCfg:getV(?workCnt), - Idx = erlang:phash2(self(), Cnt) + 1, + Idx = rand:uniform(Cnt), + erlang:start_timer(Time, ?gTimerCfg:getV(Idx), Msg). + +setTimer(Time, Msg, Strategy) -> + Cnt = ?gTimerCfg:getV(?workCnt), + Idx = ?IIF(Strategy == rand, rand:uniform(Cnt), erlang:phash2(self(), Cnt) + 1), erlang:start_timer(Time, ?gTimerCfg:getV(Idx), Msg). getTimer(TimerRef) -> erlang:read_timer(TimerRef). delTimer(TimerRef) -> - erlang:cancel_timer(TimerRef). + erlang:cancel_timer(TimerRef) .