Hi,
yes, i noticed !
but many thanks for the code.
Jan’s solution with set_stream/2 is more straightforward, but i walked into a
a weird race condition where get_single_char© will echo the character code
on the terminal (the code is a quick nasty hack). So with that, and wanting to
learn about threads a bit i looked in your code.
one important feature was that i needed the decisions to be taken in the main thread and affect the computation
path. below is some code to illustrate that, and my solution to the clean up issue.
many thanks, that was very helpful,
Nicos Angelopoulos
http://stoics.org.uk/~nicos
start :-
thread_create(check_kbd, KbdID ),
assert( check_kbd_id(KbdID) ),
my_other_goals.
check_kbd :-
read_key(T),
check_kbd( T ).
check_kbd( z ) :-
!,
thread_send_message(main,end).
check_kbd( T ) :-
thread_send_message(main,mess(T)), % main thread gets key press info
check_kbd.
kbd_active(T) :-
format('Someone typed ~w.~n',[T]).
my_other_goals :-
writeln('I woke up just to let you know I am alive'),
sleep(3),
end_not_requested.
read_key(K) :-
get_single_char(C),
atom_codes(K,[C]).
end_not_requested :-
thread_peek_message(main, mess(_M)),
!, % Fail if we got 'z' on the other thread
thread_get_message(main, Mess),
process_messages( Mess ).
end_not_requested :-
thread_peek_message(end),
!,
kill_threads.
end_not_requested :-
my_other_goals.
process_messages( mess(M) ) :-
kbd_active(M),
!,
end_not_requested.
process_messages( end ) :-
kill_threads.
kill_threads :-
check_kbd_id( Id ),
retractall( check_kbd_id(_) ),
thread_join(Id).