This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/Logtalk/examples/threads/blackboard/blackboard.lgt
pmoura bd8cca2bde Logtalk 2.29.4 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1800 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
2007-02-19 19:05:42 +00:00

67 lines
1.3 KiB
Plaintext

:- category(using). % we can call the threaded_wait/1 and threaded_notify/1 predicates from category
% predicates; the importing object threads are used for exchanging notifications
:- public([pick_up/0, release/0]).
pick_up :-
threaded_wait(free). % wait until the tool is available
release :-
threaded_notify(free). % notify that the tool is now available
:- end_category.
:- object(chalk,
imports(using)).
:- threaded. % the chalk's thread is used for exchanging notifications
:- initialization(::release). % make the chalk initially available
:- end_object.
:- object(eraser,
imports(using)).
:- threaded. % the eraser's thread is used for exchanging notifications
:- initialization(::release). % make the eraser initially available
:- end_object.
:- category(running). % in alternative to a category we could also have defined a class
:- public(run/1).
run(0) :-
!.
run(N) :-
N > 0,
eraser::pick_up,
chalk::pick_up,
self(Self),
write(Self), write(' is writing...'), nl,
random::random(1, 5, Random), % simulate a variable time
thread_sleep(Random), % spending on writing
chalk::release,
eraser::release,
N2 is N - 1,
run(N2).
:- end_category.
:- object(teacher,
imports(running)).
:- end_object.
:- object(student,
imports(running)).
:- end_object.