www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Thread in detached state?

reply Ish <ratta1i cmich.edu> writes:
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.
Nov 13 2015
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/13/2015 07:35 AM, 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.
I think the following is the idea: import std.stdio; import core.thread; extern(C) void rt_moduleTlsDtor(); void threadFunc() { writeln("Worker thread started"); thread_detachThis(); scope(exit) rt_moduleTlsDtor(); foreach (i; 0 .. 3) { Thread.sleep(1.seconds); writeln("Working..."); } writeln("Worker terminating"); } void main() { writeln("Creating thread in main"); auto worker = new Thread(&threadFunc); writeln("Starting thread in main"); worker.start(); writeln("main terminating"); } The output: Creating thread in main Starting thread in main main terminating Worker thread started Working... Working... Working... Worker terminating Ali
Nov 13 2015
parent reply Ish <ratta1i cmich.edu> writes:
On Friday, 13 November 2015 at 19:45:58 UTC, Ali Çehreli wrote:
 On 11/13/2015 07:35 AM, Ish wrote:
 [...]
I think the following is the idea: import std.stdio; import core.thread; extern(C) void rt_moduleTlsDtor(); void threadFunc() { writeln("Worker thread started"); thread_detachThis(); scope(exit) rt_moduleTlsDtor(); foreach (i; 0 .. 3) { Thread.sleep(1.seconds); writeln("Working..."); } writeln("Worker terminating"); } void main() { writeln("Creating thread in main"); auto worker = new Thread(&threadFunc); writeln("Starting thread in main"); worker.start(); writeln("main terminating"); } The output: Creating thread in main Starting thread in main main terminating Worker thread started Working... Working... Working... Worker terminating Ali
Does not produce the desired effect. For 380 threads (Linux limit) it works, for 381 threads it dies. The program termination is exactly same as in 381 threads created using spawn().
Nov 19 2015
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/19/2015 08:40 AM, Ish wrote:

 Does not produce the desired effect.
Will you tell us what that is? :)
 For 380 threads (Linux limit) it works, for 381 threads it
 dies.
As I understand it, it hit a limit. Sounds like it works as designed though. I should go read about this topic to understand what else "detached" may mean. Ali
Nov 19 2015
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/19/2015 08:40 AM, Ish wrote:

 For 380 threads (Linux limit) it works, for 381 threads it
 dies. The program termination is exactly same as in 381 threads
 created using spawn().
I've read some more about this. It looks like there are no separate counts or limits for detached versus joinable threads. Luckily, that limit can be changed: https://dustycodes.wordpress.com/2012/02/09/increasing-number-of-threads-per-process/ Ali
Nov 19 2015
prev sibling parent reply ZombineDev <valid_email he.re> writes:
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/
Nov 19 2015
parent Ish <ratta1i cmich.edu> writes:
On Thursday, 19 November 2015 at 22:07:19 UTC, ZombineDev wrote:
 On Friday, 13 November 2015 at 15:35:11 UTC, Ish wrote:
 [...]
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). [...]
Well I was trying to see how many threads could be created by a process. Linux has limit of 380 joinable threads (POSIX) per process. OS runs out of resources at this point. To increase the limit one has to recover the resources right away when a thread terminates. I will look into the links that you posted. Thanks for answering my query.
Nov 20 2015