Thread_update/2 not working?

With the following code:

tu :-
   M = amodule,
   delflag,
   thread_create(( sleep(0.5),
                   wflag(flag),
                   thread_update(true,[notify(broadcast),module(M)])
                 ),
                 _,
                 [detached(true)]),
   get_time(Bef),
   thread_wait(rflag("flag"),[db(false),module(M),retry_every(7),timeout(10)]),
   get_time(Aft),
   Diff is Aft - Bef,
   (  Diff > 0.501
   -> format('Expected wait to end in 0.5 secs, but ended in ~2f secs',[Diff])
   ;  format('good, ended in ~2f secs',[Diff])
   ).

rflag(Flag) :-
   catch(read_file_to_string('/tmp/afile',Flag,[]),_,false).

wflag(Flag) :-
   open('/tmp/afile',write,S,[]),
   write(S,Flag),
   close(S).
delflag :-
   catch(delete_file('/tmp/afile'),_,true).

Query:

25 ?- tu.
Expected wait to end in 0.5 secs, but ended in 7.50 secs
true.

Am I missing something? i would expect thread_update/2 to wake up the thread before the retry_every(Secs) happens.

The problem is that the “flag” doesn’t change anything to the module. That should indeed not be necessary when using thread_update/2, but it is still common :slight_smile: Pushed a fix that makes this work (and adds a test).

2 Likes

Great, thanks! my actual use case was changing something within an engine. It works well now!

For those wanting to see the details of the commit.

1 Like