Thread in detached state?

ZombineDev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 19 14:07:16 PST 2015


On Friday, 13 November 2015 at 15:35:11 UTC, Ish wrote:
> I was directed here from General list, so be patient with me. I 
> am looking for syntax for creating a detached-state thread in 
> the spirit of POSIX thread attribute PTHREAD_CREATE_DETACHED 
> (the thread resources are released on termination and not when 
> the main thread terminates - allows for a very large number of 
> threads).
> Sample code with call will be helpful.

If you're more familiar with pthreads, you can just use them from 
core.sys.posix.pthread [1]. After all this what core.thread uses 
on Posix [2]. In general you can use any OS functionality by 
importing the declarations you need from core.sys.[OS].[header] 
[3]. However you need to make sure to register your thread if you 
are going to use features of D that need runtime support (such as 
the GC).

I don't know if there's a good D tutorial specifically for 
pthreads, but you should be able to easily translate a C tutorial 
to D. (Because the D API is a one-to-one mapping of the C one).
For example, these two sites provide plenty of information on 
using pthreads: [4], [5]. Probably because these are targeted at 
C programmers, they wouldn't teach you the best way to use this 
functionality from D, but as you learn more about the D's 
features, you will be able to gradually develop better techniques 
for using them.

On the other hand, if you don't need to interact directly with 
Posix threads, it is recommended to use std.parallelism [6] or 
std.concurrency [7] for a more structured approach to parallelism 
and concurrency or a framework like vibe.d [8], which provides a 
well integrated approach to building scalable networking/web 
applications.

[1]: 
https://github.com/D-Programming-Language/druntime/blob/master/src/core/sys/posix/pthread.d

[2]: 
https://github.com/D-Programming-Language/druntime/blob/master/src/core/thread.d#L231

[3]: 
https://github.com/D-Programming-Language/druntime/tree/master/src/core/sys

[4]: https://computing.llnl.gov/tutorials/pthreads/
[5]: https://www.cs.cf.ac.uk/Dave/C/
[6]: http://dlang.org/phobos/std_parallelism
[7]: http://dlang.org/phobos/std_concurrency
[8]: http://vibed.org/


More information about the Digitalmars-d-learn mailing list