www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Does D support multithreading inherently?

reply Someone <Someone_member pathlink.com> writes:
Hi,
Sorry if this has been asked before. I didn't know how to search (:noob).

Does D support multithreading inherently? What is the concurrency mechanism
used?

I tried to look in the language comparison page, but couldn't find any info
about this.

Thanks you very much in advance.
May 07 2006
next sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
Depends on what you mean by inherently.

Do you mean "naturally", as in... "does D use multithreading even if my 
code never has anything to do with threads in it?"  If you do, the 
answer is no.  D does not automatically make a program threaded.

On the other hand, you may be asking, "can I write multithreaded 
programs with D, and does it make it easy?"  If this is your question, 
than the answer is yes.

For more information, I suggest you look here:

http://www.digitalmars.com/d/phobos/std_thread.html

It really is fairly trivial to write a threaded network daemon in D. 
Behind the scenes, it either uses CreateThread() and friends or POSIX 
Threads (pthread.)  However, using std.thread ensures that the garbage 
collector and your threads are properly introduced.

For concurrency, the language provides for basic locking.  For that, see 
here:

http://www.digitalmars.com/d/statement.html#synchronize

You can use synchronize for execution of just a statement or group 
thereof, or lock based on an object for multiple code blocks.  As far as 
I know, it again just uses EnterCriticalSection()/etc. or POSIX Threads.

This isn't always as much as people want, and for more there is at least 
one port of a concurrency library in progress - probably others.

Incidentally, you can search using Google.  Just try 
"site:digitalmars.com something to search for".

-[Unknown]


 Hi,
 Sorry if this has been asked before. I didn't know how to search (:noob).
 
 Does D support multithreading inherently? What is the concurrency mechanism
 used?
 
 I tried to look in the language comparison page, but couldn't find any info
 about this.
 
 Thanks you very much in advance.
 
 

May 07 2006
prev sibling parent Sean Kelly <sean f4.ca> writes:
Someone wrote:
 Hi,
 Sorry if this has been asked before. I didn't know how to search (:noob).
 
 Does D support multithreading inherently?

Sort of. D currently supports multithreading via the Thread class in std.thread, and the keyword "synchronized." There's also a "volatile" keyword, but it doesn't do what you'd expect and is really more useful for lock-free programming. So at the moment, D is fairly well suited for general multithreaded programming but has little in the way of specialized concurrency support.
 What is the concurrency mechanism used?

Threads wrapped in library code, for the most part. There has been some talk on this forum of experimenting with additional concurrency support, but nothing solid yet. Sean
May 08 2006