Thread overview
D and parallel programming
Aug 06, 2007
John Burak
Aug 06, 2007
Sean Kelly
Aug 07, 2007
Regan Heath
Aug 06, 2007
Robert Fraser
August 06, 2007
I am looking into D as a possibility for a programming project I want to start.  Can someone point me to resources on how D supports parallel programming, assuming it does?  I'm surprised I didn't notice anything about parallel programming on the "d language overview" page when I scanned/searched it.  Unless it is there and I missed it (quite possible) then I think it should be added so people know what D is capable of (I'm still assuming it has some native support for parallel programming).

-John
August 06, 2007
John Burak wrote:
> I am looking into D as a possibility for a programming project I want to start.  Can someone point me to resources on how D supports parallel programming, assuming it does?  I'm surprised I didn't notice anything about parallel programming on the "d language overview" page when I scanned/searched it.  Unless it is there and I missed it (quite possible) then I think it should be added so people know what D is capable of (I'm still assuming it has some native support for parallel programming).

Support for multithreaded programming (what I assume you meant by "parallel programming") in D largely amounts to a standard Thread object in the library, as well as a "synchronized" keyword which behaves quite similar to the one in Java.  There is also a "volatile" keyword, but it is really only intended for lock-free programming and thus doesn't see much use outside of library code.

If it helps, Tango also contains a Fiber implementation, a read-write mutex, condition variables, and semaphores.  There is also a coroutine implementation and a CSP implementation at http://www.assertfalse.com.

I think we still have a ways to go for easily usable concurrency in D, but the basic building blocks are there.


Sean
August 06, 2007
John Burak Wrote:

> I am looking into D as a possibility for a programming project I want to start.  Can someone point me to resources on how D supports parallel programming, assuming it does?  I'm surprised I didn't notice anything about parallel programming on the "d language overview" page when I scanned/searched it.  Unless it is there and I missed it (quite possible) then I think it should be added so people know what D is capable of (I'm still assuming it has some native support for parallel programming).
> 
> -John

It has a synchronized keyword and threads in both standard libraries (Tango's threading support is a bit more robust, as it has futures and fibers)...
August 07, 2007
Sean Kelly wrote:
> Support for multithreaded programming (what I assume you meant by "parallel programming") in D largely amounts to a standard Thread object in the library, as well as a "synchronized" keyword which behaves quite similar to the one in Java.  

Except that (importantly, IMO) it's re-entrant.
By that I mean, if you have:

class Foo
{
  int i;

  void bar()
  {
    synchronized(this)
    {
      i++;
    }
  }
}

void main()
{
  Foo f = new Foo();
  synchronized(f)
  {
    f.bar();
  }
}

you get no deadlock.  As I believe they do in Java (someone correct me if I'm wrong).

In short, multiple synchronized blocks, on the same object, in the same thread are allowed and do not cause a deadlock.

Anyone know what the C# behaviour is in this case?

Regan
August 07, 2007
Regan Heath wrote:
> Anyone know what the C# behaviour is in this case?

using System;

class Foo
{
	int i;
	public void bar()
	{
		lock (this)
		{
			i++;
		}
	}

	public static void Main()
	{
		Foo f = new Foo();
		lock (f)
		{
			f.bar();
		}
	}
}

It works but it is always recommended to lock on a private object because there are some corner cases where it will dead-lock. Something like the following is always preferred:

	readonly object syncRoot = new object();

	(...)
	lock (syncRoot)
	{
		(...)
	}