dlibgit sample - concurrency issues

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Oct 11 17:00:14 PDT 2012


On 10/12/12, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> There is a minimal git client sample
> ported from C (it has just a few commands implemented).

Ok guys I could use some help here. Take a look at these (ported from
C which used pthreads and didn't really care about thread safety):
https://github.com/AndrejMitrovic/dlibgit/blob/master/samples/network/clone.d#L43
https://github.com/AndrejMitrovic/dlibgit/blob/master/samples/network/common.d#L26

Here's the original C sample:
https://github.com/libgit2/libgit2/blob/development/examples/network/clone.c
(uses pthread_create)

The situation is:
- Main thread prepares some struct data type
- This is the type:
https://github.com/AndrejMitrovic/dlibgit/blob/master/samples/network/common.d#L14
- Main thread spawns a worker thread that has to write to the struct
data type (the type has indirections due to string fields).
- Worker thread reads from and writes to this data type.
- Main thread spins and reads from the data type until a bool flag is
set, after which it knows the work thread is done and the main thread
can break out of the loop.

I can't put the struct variable in TLS and use spawn() to pass a
pointer to it ("Aliases to mutable thread-local data not allowed."),
so I've had to use a __gshared global instead and let the spawned
function access it implicitly.

I could type the struct variable as shared() but I'd have to cast away
the shared qualifier when calling git_clone, which is a C function
(https://github.com/AndrejMitrovic/dlibgit/blob/master/samples/network/clone.d#L17).

Question is: Isn't there a better way to model this, without using
globals or casts? I'd love to see a good thread-safe example on how to
implement this.


More information about the Digitalmars-d mailing list