www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Synchronized Linked List Traversal

reply Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
I'm working on a project that requires multi-threading and I am trying to grasp 
the best way to work with setting up a linked-list where multiple threads might 
try to be setting the next element in the list at the same time.

What I really need to know is if this will work:

Node!(T) child = parent.firstChild;
synchronized (child)
{
     while (child.sibling !is null)
         child = child.sibling;

     child.sibling = node;
}

What I am worried about is that the mutex will not follow the traversal. Any 
help would be great.
Thanks you,
Jonathan
Jan 20 2010
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 20 Jan 2010 19:25:27 -0500, Jonathan Crapuchettes  
<jcrapuchettes gmail.com> wrote:

 I'm working on a project that requires multi-threading and I am trying  
 to grasp the best way to work with setting up a linked-list where  
 multiple threads might try to be setting the next element in the list at  
 the same time.

 What I really need to know is if this will work:

 Node!(T) child = parent.firstChild;
 synchronized (child)
 {
      while (child.sibling !is null)
          child = child.sibling;

      child.sibling = node;
 }

 What I am worried about is that the mutex will not follow the traversal.  
 Any help would be great.

It will not follow the traversal. Unfortunately, you cannot use the synchronized keyword since you cannot do interleaving locks, you must manually lock in this pattern: lock(child) while(child.sibling !is null) { auto tmp = child; child = child.sibling; lock(child); unlock(tmp); } child.sibling = node; unlock(child); To do this, you need a manual mutex, look in tango.core.sync.Mutex I think. But it might also be a better thing to have parent save a pointer to the end of the list so you don't traverse the list for every addition... -Steve
Jan 21 2010
parent reply Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
Thank you for the help. Since I am using D1, I think I will use the pthread 
mutexes through the C API.

Steven Schveighoffer wrote:
 On Thu, 21 Jan 2010 08:35:50 -0500, Steven Schveighoffer
 <schveiguy yahoo.com> wrote:

 To do this, you need a manual mutex, look in tango.core.sync.Mutex I
 think.

or core.sync.mutex for D2... -Steve

Jan 21 2010
parent Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
Yeah, but I have a lot of code in this project that uses phobos and it would be 
a major pain (not to mention the time) to switch over.

Thank you for the thought,
Jonathan

Steven Schveighoffer wrote:
 On Thu, 21 Jan 2010 13:29:37 -0500, Jonathan Crapuchettes
 <jcrapuchettes gmail.com> wrote:

 Thank you for the help. Since I am using D1, I think I will use the
 pthread mutexes through the C API.

Tango provides mutex objects for D1. -Steve

Jan 21 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 21 Jan 2010 08:35:50 -0500, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 To do this, you need a manual mutex, look in tango.core.sync.Mutex I  
 think.

or core.sync.mutex for D2... -Steve
Jan 21 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 21 Jan 2010 13:29:37 -0500, Jonathan Crapuchettes  
<jcrapuchettes gmail.com> wrote:

 Thank you for the help. Since I am using D1, I think I will use the  
 pthread mutexes through the C API.

Tango provides mutex objects for D1. -Steve
Jan 21 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 21 Jan 2010 13:44:53 -0500, Jonathan Crapuchettes  
<jcrapuchettes gmail.com> wrote:

 Yeah, but I have a lot of code in this project that uses phobos and it  
 would be a major pain (not to mention the time) to switch over.

 Thank you for the thought,
 Jonathan

No problem. You also might consider tangobos, but I'm not sure how updated it is WRT tango. -Steve
Jan 21 2010