Logtalk 2.29.4 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1800 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
14
Logtalk/examples/threads/blackboard/NOTES.txt
Normal file
14
Logtalk/examples/threads/blackboard/NOTES.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.29.4
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
To load this example and for sample queries, please see the SCRIPT file.
|
||||
|
||||
This folder contains a simple multi-threading example illustrating how
|
||||
to use the Logtalk built-in predicates threaded_wait/1 and threaded_notify/1
|
||||
for synchronizing threads using shared resources. The example consists of
|
||||
two persons, a student and a teacher, sharing a blackboard chalk and eraser.
|
38
Logtalk/examples/threads/blackboard/SCRIPT.txt
Normal file
38
Logtalk/examples/threads/blackboard/SCRIPT.txt
Normal file
@@ -0,0 +1,38 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.29.4
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
% start by loading the necessary library support files:
|
||||
|
||||
| ?- logtalk_load(library(random_loader)).
|
||||
...
|
||||
|
||||
|
||||
% now you are ready for loading the example:
|
||||
|
||||
| ?- logtalk_load(blackboard(loader)).
|
||||
...
|
||||
|
||||
|
||||
% start the producer and the consumer, each one running in its own thread:
|
||||
|
||||
| ?- threaded_ignore(teacher::run(4)), threaded_ignore(student::run(10)).
|
||||
|
||||
teacher is writing...
|
||||
student is writing...
|
||||
student is writing...
|
||||
student is writing...
|
||||
student is writing...
|
||||
teacher is writing...
|
||||
teacher is writing...
|
||||
teacher is writing...
|
||||
student is writing...
|
||||
student is writing...
|
||||
student is writing...
|
||||
student is writing...
|
||||
student is writing...
|
||||
student is writing...
|
66
Logtalk/examples/threads/blackboard/blackboard.lgt
Normal file
66
Logtalk/examples/threads/blackboard/blackboard.lgt
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
:- 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.
|
13
Logtalk/examples/threads/blackboard/loader.lgt
Normal file
13
Logtalk/examples/threads/blackboard/loader.lgt
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load(
|
||||
blackboard)).
|
||||
|
||||
/*
|
||||
If you intend to use the FOP XSL:FO processor for generating PDF documenting
|
||||
files, comment the directive above and uncomment the directive below
|
||||
|
||||
:- initialization(
|
||||
logtalk_load(
|
||||
blackboard, [xmlsref(standalone)])).
|
||||
*/
|
14
Logtalk/examples/threads/buffer/NOTES.txt
Normal file
14
Logtalk/examples/threads/buffer/NOTES.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.29.4
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
To load this example and for sample queries, please see the SCRIPT file.
|
||||
|
||||
This folder contains a simple multi-threading example illustrating how
|
||||
to use the Logtalk built-in predicates threaded_wait/1 and threaded_notify/1
|
||||
for synchronizing threads writing to and reading from a buffer that can
|
||||
only contain an item at the same time.
|
44
Logtalk/examples/threads/buffer/SCRIPT.txt
Normal file
44
Logtalk/examples/threads/buffer/SCRIPT.txt
Normal file
@@ -0,0 +1,44 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.29.4
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
% start by loading the necessary library support files:
|
||||
|
||||
| ?- logtalk_load(library(random_loader)).
|
||||
...
|
||||
|
||||
|
||||
% now you are ready for loading the example:
|
||||
|
||||
| ?- logtalk_load(buffer(loader)).
|
||||
...
|
||||
|
||||
|
||||
% start the producer and the consumer, each one running in its own thread:
|
||||
|
||||
| ?- threaded_ignore(producer::run(10)), threaded_ignore(consumer::run(10)).
|
||||
|
||||
producer wrote item 0
|
||||
consumer read item 0
|
||||
producer wrote item 1
|
||||
consumer read item 1
|
||||
producer wrote item 2
|
||||
consumer read item 2
|
||||
producer wrote item 3
|
||||
consumer read item 3
|
||||
producer wrote item 4
|
||||
consumer read item 4
|
||||
producer wrote item 5
|
||||
consumer read item 5
|
||||
producer wrote item 6
|
||||
consumer read item 6
|
||||
producer wrote item 7
|
||||
consumer read item 7
|
||||
producer wrote item 8
|
||||
consumer read item 8
|
||||
producer wrote item 9
|
||||
consumer read item 9
|
66
Logtalk/examples/threads/buffer/buffer.lgt
Normal file
66
Logtalk/examples/threads/buffer/buffer.lgt
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
:- object(buffer).
|
||||
|
||||
:- threaded.
|
||||
|
||||
:- public([put/1, get/1]).
|
||||
|
||||
:- private(item/1).
|
||||
:- dynamic(item/1).
|
||||
|
||||
put(N) :-
|
||||
( N > 0 % wait until the previous item is consumed
|
||||
-> NP is N - 1, threaded_wait(consumed(NP)) % (except for the first item!)
|
||||
; true
|
||||
),
|
||||
assertz(item(N)),
|
||||
sender(Sender),
|
||||
writeq(Sender), write(' wrote item '), write(N), nl,
|
||||
threaded_notify(produced(N)). % notify consumer that a new item is available
|
||||
|
||||
get(N) :-
|
||||
threaded_wait(produced(N)), % wait until an item is available
|
||||
retract(item(N)),
|
||||
sender(Sender),
|
||||
writeq(Sender), write(' read item '), write(N), nl,
|
||||
threaded_notify(consumed(N)). % notify producer that the item was consumed
|
||||
|
||||
:- end_object.
|
||||
|
||||
|
||||
:- object(producer).
|
||||
|
||||
:- public(run/1).
|
||||
|
||||
run(N) :-
|
||||
run(0, N).
|
||||
|
||||
run(N, N) :- !.
|
||||
run(M, N) :-
|
||||
M < N,
|
||||
random::random(1, 5, Random), % simulate a variable time to
|
||||
thread_sleep(Random), % produce a new item
|
||||
buffer::put(M),
|
||||
M2 is M + 1,
|
||||
run(M2, N).
|
||||
|
||||
:- end_object.
|
||||
|
||||
|
||||
:- object(consumer).
|
||||
|
||||
:- public(run/1).
|
||||
|
||||
run(N) :-
|
||||
run(0, N).
|
||||
|
||||
run(N, N) :- !.
|
||||
run(M, N) :-
|
||||
M < N,
|
||||
random::random(1, 5, Random), % simulate a variable time
|
||||
thread_sleep(Random), % to consume an item
|
||||
buffer::get(M),
|
||||
M2 is M + 1,
|
||||
run(M2, N).
|
||||
|
||||
:- end_object.
|
13
Logtalk/examples/threads/buffer/loader.lgt
Normal file
13
Logtalk/examples/threads/buffer/loader.lgt
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load(
|
||||
buffer)).
|
||||
|
||||
/*
|
||||
If you intend to use the FOP XSL:FO processor for generating PDF documenting
|
||||
files, comment the directive above and uncomment the directive below
|
||||
|
||||
:- initialization(
|
||||
logtalk_load(
|
||||
buffer, [xmlsref(standalone)])).
|
||||
*/
|
Reference in New Issue
Block a user