Re: threads, queues, and make

Paulo Moura <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 10/10/2013, at 08:51, Jan Wielemaker <[email protected]> wrote:

> On 10/10/2013 04:07 AM, Anne Ogborn wrote:
>> I'm writing a logger that runs in it's own thread and communicates via message queues.
>> The logger keeps some running statistics. I don't want to assert these for performance reasons,
>> so the separate thread is easiest.
>> 
>> start_logger_thread :-
>> message_queue_create(logger_response_queue),
>> thread_create(logger_thread,
>>     _,
>>     [
>> alias(my_logger),
>> detached(true)
>>     ]).
>> 
>> 
>> The problem comes in maintaining a pleasant development environment.
>> 
>> I'd like to continue to have make. update my code happily, but of course the thread and the message
>> queue exist, and the system whines when I try to redefine them.
>> 
>> At the moment I'm just catching the exception, but it'd be better to detect the existance of the queue before creating it. There doesn't seem to be a queue_exists?
> 
> message_queue_property/2 can be used for that.

Yes but:

?- message_queue_property(fdgfdsgdg, P).
ERROR: message_queue_property/2: message_queue `fdgfdsgdg' does not exist

Same on YAP. XSB doesn't have yet this predicate (considering adding it, however). Maybe message_queue_property/2 should behave instead as predicate_property/2 in that it fails if the first argument doesn't exist?

Cheers,

Paulo

>  Same for
> thread_property/2 to test for the
> existince of the thread.  You could also have a log/1 predicate that
> creates the thread
> lazily.  Something like this (not tested; details may be wrong):
> 
> log(Term) :-
> 	catch(thread_send_message(logger_response_queue, Term),
> 	      error(existence_error(message_queue, _, _),
> 	      fail), !.
> log(Term) :-
> 	with_mutex(create_log_thread, start_logger_thread),
> 	log(Term).
> 
> 	Cheers --- Jan

-----------------------------------------------------------------
Paulo Moura
Logtalk developer

Email: <mailto:[email protected]>
Web:   <http://logtalk.org/>
-----------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.