```D
import std;
import core.sys.posix.unistd : sleep;

void foo (size_t i)
{
   writeln (i);
   sleep (1);
   writefln ("thread %d end", i);
}

void main (string [] args)
{
   auto nthreads = args [1].to!size_t;

   Tid [] threads;
   foreach (i; 0 .. nthreads)
      threads ~= spawn (&foo, i);

   writeln ("main end");
}
```

If this code is started with the argument of `8` it runs and prints

```
0
main end
1
7
4
5
3
6
2
thread 0 end
thread 1 end
thread 7 end
thread 4 end
thread 5 end
thread 3 end
thread 6 end
thread 2 end
```

before control is transfered back to the shell. But if I start the program with sufficiently low setting of the maximum number of processes (say `ulimit -u 120`) the process prints

```
0
core.thread.threadbase.ThreadError@src/core/thread/threadbase.d(1224): Error 
creating thread
----------------
2
1
6
4
5
3
thread 0 end
thread 2 end
thread 1 end
thread 6 end
thread 4 end
thread 5 end
thread 3 end
```

and then hangs. According to gdb there is only one thread running which shows the following stackdump:

```
(gdb) bt
#0 0x00007ffff6a5ecb7 in sched_yield () at ../sysdeps/unix/syscall-template.S:81 #1 0x00007ffff7c3b80d in core.thread.osthread.thread_yield() () from [...]/linux/lib64/libphobos2.so.0.110 #2 0x00007ffff7c3a151 in thread_joinAll () from [...]/linux/lib64/libphobos2.so.0.110 #3 0x00007ffff7c43d55 in rt_term () from [...]/linux/lib64/libphobos2.so.0.110 #4 0x00007ffff7c442fd in rt.dmain2._d_run_main2() () from [...]/linux/lib64/libphobos2.so.0.110 #5 0x00007ffff7c441e6 in rt.dmain2._d_run_main2() () from [...]/linux/lib64/libphobos2.so.0.110 #6 0x00007ffff7c4414f in _d_run_main2 () from [...]/linux/lib64/libphobos2.so.0.110 #7 0x00007ffff7c43f38 in _d_run_main () from [...]/linux/lib64/libphobos2.so.0.110
#8  0x0000000000415226 in main (argc=2, argv=0x7fffffffdd38)
at [...]/linux/bin64/../../src/druntime/import/core/internal/entrypoint.d:29
```

It appears to me that there is an endless loop of calls to `sched_yield`. Do I miss something?

Reply via email to