digitalmars.D.learn - Thread in detached state?
- Ish (7/7) Nov 13 2015 I was directed here from General list, so be patient with me. I
- =?UTF-8?Q?Ali_=c3=87ehreli?= (32/38) Nov 13 2015 I think the following is the idea:
- Ish (4/37) Nov 19 2015 Does not produce the desired effect. For 380 threads (Linux
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/9) Nov 19 2015 As I understand it, it hit a limit. Sounds like it works as designed
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/10) Nov 19 2015 I've read some more about this. It looks like there are no separate
- ZombineDev (34/41) Nov 19 2015 If you're more familiar with pthreads, you can just use them from
- Ish (7/17) Nov 20 2015 Well I was trying to see how many threads could be created by a
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
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
On Friday, 13 November 2015 at 19:45:58 UTC, Ali Çehreli wrote:On 11/13/2015 07:35 AM, Ish wrote: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().[...]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 19 2015
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
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
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
On Thursday, 19 November 2015 at 22:07:19 UTC, ZombineDev wrote:On Friday, 13 November 2015 at 15:35:11 UTC, Ish wrote: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.[...]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). [...]
Nov 20 2015