notification across threads, with tools

downs default_357-line at yahoo.de
Tue May 13 10:32:19 PDT 2008


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


More information about the Digitalmars-d-learn mailing list