Thanks Thomas for the enlightening infos!
I have tested your code and it worked well. My test:
Code: Select all
class myClass
domains
buf = buf(arrayM{unsigned} ArrayM_a, arrayM{unsigned} ArrayM_b).
predicates
getBuf : () -> buf Buf.
properties
bufMapM : mapM{multiThread_native::threadId ThreadId, buf Buf}.
end class myClass
%---
implement myClass
class facts
bufMapM : mapM{multiThread_native::threadId ThreadId, buf Buf} := mapM_redBlack_cas::new() [immediate].
clauses
getBuf() = Buf :-
ThreadHandle = multiThread_native::getCurrentThread(),
ThreadID = multiThread_native::getThreadID(ThreadHandle),
if Buf = bufMapM:tryGet(ThreadId) then
else
Buf = buf(arrayM::new(0), arrayM::new(0)),
bufMapM:set(ThreadId, Buf),
ActualThreadHandle = multiThread_api::mkNonInheritable(ThreadHandle, :CloseOriginal = false),
SyncObject = syncObject::new(ActualThreadHandle, :NeedRelease = false),
% we close the object explicitly below
_Future =
pfc\asynchronous\future::submit_unit(
{ :-
_ = SyncObject:waitAsync(),
SyncObject:close(),
bufMapM:removeKey(ThreadID)
},
:Ctx = pfc\asynchronous\executionContext_pool::defaultPool)
end if.
end implement myClass
%======
implement main
clauses
run() :-
_Future1 =
pfc\asynchronous\future::submit_unit(
{ :-
programControl::sleep(1000),
_Buf = myClass::getBuf()
}),
_Future2 =
pfc\asynchronous\future::submit_unit(
{ :-
programControl::sleep(4000),
_Buf = myClass::getBuf()
}),
_Future3 =
pfc\asynchronous\future::submit_unit(
{ :-
programControl::sleep(7000),
_Buf = myClass::getBuf()
}),
watchLoop(0).
class predicates
watchLoop : (unsigned Num).
clauses
watchLoop(Num) :-
stdIO::write(Num, " seconds: "),
stdIO::present(myClass::bufMapM),
stdIO::nl(),
programControl::sleep(1000),
watchLoop(Num + 1).
end implement main
The output was:
Code: Select all
0 seconds: []
1 seconds: [26244 -> buf([], [])]
2 seconds: [26244 -> buf([], [])]
3 seconds: [26244 -> buf([], [])]
4 seconds: [20260 -> buf([], []), 26244 -> buf([], [])]
5 seconds: [20260 -> buf([], []), 26244 -> buf([], [])]
6 seconds: [20260 -> buf([], []), 26244 -> buf([], [])]
7 seconds: [20260 -> buf([], []), 22548 -> buf([], []), 26244 -> buf([], [])]
8 seconds: [20260 -> buf([], []), 22548 -> buf([], []), 26244 -> buf([], [])]
9 seconds: [20260 -> buf([], []), 22548 -> buf([], []), 26244 -> buf([], [])]
10 seconds: [20260 -> buf([], []), 22548 -> buf([], []), 26244 -> buf([], [])]
11 seconds: [20260 -> buf([], []), 22548 -> buf([], []), 26244 -> buf([], [])]
12 seconds: [20260 -> buf([], []), 22548 -> buf([], []), 26244 -> buf([], [])]
13 seconds: [20260 -> buf([], []), 22548 -> buf([], []), 26244 -> buf([], [])]
14 seconds: [22548 -> buf([], [])]
15 seconds: [22548 -> buf([], [])]
16 seconds: [22548 -> buf([], [])]
17 seconds: [22548 -> buf([], [])]
18 seconds: [22548 -> buf([], [])]
19 seconds: [22548 -> buf([], [])]
20 seconds: [22548 -> buf([], [])]
21 seconds: [22548 -> buf([], [])]
22 seconds: [22548 -> buf([], [])]
23 seconds: [22548 -> buf([], [])]
24 seconds: []
25 seconds: []
26 seconds: []
...