Sign up

GTKD and Multithreading

I am working on a utility to provide a UI around grep. Getting the basic functionality up and running was pretty straight forward but now I want the searching files to happen in a background thread while updating the UI dynamically.

Looking at the documentation, it looks like the best way to to this is to use D's message passing features to send messages from the spawned thread to a function bound to GTK's threadsAddIdle which would perform the receive. I put together a small program to test this out and it seems to work well enough so it's probably the way I'll go. However I did have some questions as follows:

  1. Ideally it would be nice to bind a delegate to threadsAddIdle instead of a C style function. I found code to do this from the grestful project (https://github.com/Gert-dev/grestful/blob/master/Generic/Utility.d scroll to bottom of file) and it works well. Does GTKD have something similar built into it, I looked around but nothing obvious came out of it.

The grestful functions work but has a few wrinkles, specifically I'd like to see something where the delegate could return a bool to determine whether to be removed from future idle notifications. Right now the grestful implementation has it hard coded so the delegate receives one notification and is then removed by hard coding a return false in the extern C function it binds to threadsAddIdle. I've tweaked it so that it returns true to keep receiving the idle notifications unless the delegate throws an exception but I hate using exceptions for flow control.

  1. Are there any multi-threading examples in GTKD, I love to compare what I'm playing around with to alternative implementations. Being a complete newbie to D and GTKD I'm sure what I have so far could be vastly improved plus there may be gotcha's that looking at some other code would be helpful.

Re: GTKD and Multithreading

On Wed, 28 Oct 2015 19:13:27 GMT, Gerald Nunn wrote:

I am working on a utility to provide a UI around grep. Getting the basic functionality up and running was pretty straight forward but now I want the searching files to happen in a background thread while updating the UI dynamically.

Looking at the documentation, it looks like the best way to to this is to use D's message passing features to send messages from the spawned thread to a function bound to GTK's threadsAddIdle which would perform the receive. I put together a small program to test this out and it seems to work well enough so it's probably the way I'll go. However I did have some questions as follows:

That's indeed the best way to do it.

  1. Ideally it would be nice to bind a delegate to threadsAddIdle instead of a C style function. I found code to do this from the grestful project (https://github.com/Gert-dev/grestful/blob/master/Generic/Utility.d scroll to bottom of file) and it works well. Does GTKD have something similar built into it, I looked around but nothing obvious came out of it.

Currently no, but it would be a good addition.

The grestful functions work but has a few wrinkles, specifically I'd like to see something where the delegate could return a bool to determine whether to be removed from future idle notifications. Right now the grestful implementation has it hard coded so the delegate receives one notification and is then removed by hard coding a return false in the extern C function it binds to threadsAddIdle. I've tweaked it so that it returns true to keep receiving the idle notifications unless the delegate throws an exception but I hate using exceptions for flow control.

The gtk signals use something similar, would be a good addition.

  1. Are there any multi-threading examples in GTKD, I love to compare what I'm playing around with to alternative implementations. Being a complete newbie to D and GTKD I'm sure what I have so far could be vastly improved plus there may be gotcha's that looking at some other code would be helpful.

There are currently no multi threading examples.

Re: GTKD and Multithreading

  1. Are there any multi-threading examples in GTKD, I love to compare what I'm playing around with to alternative implementations. Being a complete newbie to D and GTKD I'm sure what I have so far could be vastly improved plus there may be gotcha's that looking at some other code would be helpful.

There are currently no multi threading examples.

Thanks Mike, I'll try to clean up and simplify a bit the test program I did and submit for possible inclusion in the next couple of weeks via a pull request.

Re: GTKD and Multithreading

On 11/03/2015 12:16 AM, Gerald Nunn wrote:

  1. Are there any multi-threading examples in GTKD, I love to compare what I'm playing around with to alternative implementations. Being a complete newbie to D and GTKD I'm sure what I have so far could be vastly improved plus there may be gotcha's that looking at some other code would be helpful.

There are currently no multi threading examples.

Thanks Mike, I'll try to clean up and simplify a bit the test program I did and submit for possible inclusion in the next couple of weeks via a pull request.

Great.