-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpmt1.erl
More file actions
67 lines (59 loc) · 1.92 KB
/
mpmt1.erl
File metadata and controls
67 lines (59 loc) · 1.92 KB
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
61
62
63
64
65
66
%
% mpmt1.erl: A stupid simple example of Erlang threading
%
% License:
% Apache License, Version 2.0
% History:
% * 2024/05/18 v0.1 Initial version
% Author:
% Masanori Itoh <masanori.itoh@gmail.com>
% Usage:
% $ erlc mpmt1.erl
% $ erl -noshell -pa mpmt1.beam -s mpmt1 start NUM_CTX DURATION -s init stop
% NUM_CTX : number of threads
% DURATION : duration in seconds
% TODO:
% * Enable to flush remaining messages in busy_worker()
% * Use argparse
% * Use message to synchronize waiting for thread termination
%
-module(mpmt1).
-export([start/1, busy_worker/3, create_busy_worker/2]).
busy_worker(Idx, Current_ns, Timeleft_ns) ->
if
Timeleft_ns =< 0 ->
io:format("Expired: ~p ~p ~n", [Idx, Timeleft_ns]),
%exit(normal);
ok;
true ->
Now = erlang:system_time(),
busy_worker(Idx, Now, Now - Current_ns)
end.
create_busy_worker(0, _) ->
ok;
create_busy_worker(Num_context, Duration) ->
io:format("create_busy_worker(): Idx: ~p~n", [Num_context]),
% system_time() returns the timestamp in nano-seconds
Now = erlang:system_time(),
spawn(mpmt1, busy_worker, [Num_context, Now, Duration * 1000 * 1000 * 1000]),
create_busy_worker(Num_context - 1, Duration).
start(Args) ->
Duration =
if
length(Args) >=2 ->
erlang:list_to_integer(atom_to_list(lists:nth(2, Args)));
true ->
5
end,
Num_context =
if
length(Args) >=1 ->
erlang:list_to_integer(atom_to_list(lists:nth(1, Args)));
true ->
4
end,
io:format("NUM_CONTEXT: ~p DURATION: ~p~n", [Num_context, Duration]),
create_busy_worker(Num_context, Duration),
% TODO(thatsdone): Use message to synchronize with threads.
io:format("start(): sleeping ~p sec.~n", [Duration + 1]),
timer:sleep((Duration + 1) * 1000).