www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - notification across threads

reply Luke <fake fakemail.fake> writes:
I am pretty new to D but I'm enjoying the language so far. I'm writing an
application (basically just for learning purposes) in which I create a new
thread and then want my main thread to do some work (update the GUI) after the
spawned thread has completed. The main thread has a DWT GUI running so I do not
want that to be locked up until the "worker" thread completes. I tried using
signals/slots but the slot code gets executed in the same thread from which the
signal is emitted. Is there a way to have that slot code executed in the main
thread or something similar?
May 13 2008
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to luke,

 I am pretty new to D but I'm enjoying the language so far. I'm writing
 an application (basically just for learning purposes) in which I
 create a new thread and then want my main thread to do some work
 (update the GUI) after the spawned thread has completed. The main
 thread has a DWT GUI running so I do not want that to be locked up
 until the "worker" thread completes. I tried using signals/slots but
 the slot code gets executed in the same thread from which the signal
 is emitted. Is there a way to have that slot code executed in the main
 thread or something similar?
 

One option would be to put a thread safe queue of delegates in the main thread. Then the slot code just stuffs an action into it. Then the main thread, at some point, run everything in the queue: while(queue.notEmpty) queue.dequeue(); not to perdy but it would get the job done for some cases. What would be niceer would be some sort of interupt, but I don't known how to do that.
May 13 2008
parent downs <default_357-line yahoo.de> writes:
BCS wrote:
 Reply to luke,
 
 I am pretty new to D but I'm enjoying the language so far. I'm writing
 an application (basically just for learning purposes) in which I
 create a new thread and then want my main thread to do some work
 (update the GUI) after the spawned thread has completed. The main
 thread has a DWT GUI running so I do not want that to be locked up
 until the "worker" thread completes. I tried using signals/slots but
 the slot code gets executed in the same thread from which the signal
 is emitted. Is there a way to have that slot code executed in the main
 thread or something similar?

One option would be to put a thread safe queue of delegates in the main thread. Then the slot code just stuffs an action into it. Then the main thread, at some point, run everything in the queue: while(queue.notEmpty) queue.dequeue(); not to perdy but it would get the job done for some cases. What would be niceer would be some sort of interupt, but I don't known how to do that.

You can exploit scrapple.tools' threadpools to do that - just create an empty ThreadPool (auto main_tp = new ThreadPool(0)), then occasionally, in the main thread, run "main_tp.idle(); ". Then in the spawned thread, just main_tp.addTask({ /* task code goes here */ }); Or if you want the spawned thread to wait until the task has finished: "main_tp.future({ /* code goes here */ }).eval();" --downs
May 13 2008
prev sibling parent reply Luke <fake fakemail.fake> writes:
Luke Wrote:

 I am pretty new to D but I'm enjoying the language so far. I'm writing an
application (basically just for learning purposes) in which I create a new
thread and then want my main thread to do some work (update the GUI) after the
spawned thread has completed. The main thread has a DWT GUI running so I do not
want that to be locked up until the "worker" thread completes. I tried using
signals/slots but the slot code gets executed in the same thread from which the
signal is emitted. Is there a way to have that slot code executed in the main
thread or something similar?
 

Thanks for the suggestions. Naturally, I read them and then decided to try something else. :) I've figured out something that works but I'd be interested to know if anyone advises against using this approach. Since I'm using DWT, I use "asyncExec" to emit my signal when the thread function completes. The code in the associated slot then gets run in the main thread and everything seems to work. private void threadFunc() { Stdout.formatln("thread starting..."); Thread.sleep(10); Stdout.formatln("{} finished", Thread.getThis.name); m_display.asyncExec(new class () Runnable { public void run() { this.outer.finishedThread(++this.outer.threadFinishedCounter); } }); } One thing I don't like about this is that now my non-Gui code has to know about some DWT stuff (Display & Runnable). Any other drawbacks to this? Thanks again.
May 13 2008
parent Luke <fake fakemail.fake> writes:
Luke Wrote:

 One thing I don't like about this is that now my non-Gui code has to know
about some DWT stuff (Display & Runnable). Any other drawbacks to this?
 

Shortly after posting I realized that this concern was unfounded. It was quite simple to just call the signal and then move the asyncExec stuff to the slot function. I must say, D + Tango + DWT + DSSS is turning out to be a very nice set of tools.
May 14 2008