Re: threads, queues, and make

Jan Wielemaker <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
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.  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


> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
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.