www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sleeping vs sched_yield

reply Chris Katko <ckatko gmail.com> writes:
there's:

```d
   import core.thread;
   Thread.sleep( dur!("msecs")(10) );
```

but what if you want to simply yield all remaining time back to 
the time scheduler?

Is there a D std.library accessible version of POSIX sched_yield:

   https://man7.org/linux/man-pages/man2/sched_yield.2.html


It seems I can (thanks to the amazing work of D community) simply 
do:

```d
extern(C) int sched_yield(void);  // #include <sched.h>
```

however, how does the linker know I need <sched.h> and not some 
local library, or SDL library, or SDL2.0 library, etc. Shouldn't 
I be specifying the library somewhere?





Side side question: The above line fails to compile as-is because 
it has (void) instead of ().

```
source/app.d(226,16): Error: cannot have parameter of type `void`
```

Should that be corrected in the compiler? Shouldn't () and (void) 
be interchangeable as long as you're not doing void*?
Dec 02 2021
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Dec 02, 2021 at 11:29:17PM +0000, Chris Katko via Digitalmars-d-learn
wrote:
[...]
 It seems I can (thanks to the amazing work of D community) simply do:
 
 ```d
 extern(C) int sched_yield(void);  // #include <sched.h>
 ```
 
 however, how does the linker know I need <sched.h> and not some local
 library, or SDL library, or SDL2.0 library, etc. Shouldn't I be
 specifying the library somewhere?
An extern(C) function exists in the global namespace, namely, `sched_yield` will bind at link time to whatever library exports the symbol `sched_yield`. Most linkers (and linker configurations) will generate an error if there are multiple symbols with the same name defined. (The exception is when a symbol is marked as a "weak" symbol, but that's usually not done except for special purposes.) This is why good libraries like SDL always prefixes their API functions with `SDL_`, for example. In order to prevent confusion of SDL functions with a function of the same name from another library. [...]
 Side side question: The above line fails to compile as-is because it
 has (void) instead of ().
 
 ```
 source/app.d(226,16): Error: cannot have parameter of type `void`
 ```
 
 Should that be corrected in the compiler? Shouldn't () and (void) be
 interchangeable as long as you're not doing void*?
In C, `int func()` means `func` can take any number of arguments. That's why you need to write `int func(void)` to explicitly say there are NO parameters. But in D, `int func()` means `func` takes no arguments. So you never write `int func(void)`. D's `int func()` == C's `int func(void)`. T -- There are four kinds of lies: lies, damn lies, and statistics.
Dec 02 2021
prev sibling next sibling parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Thursday, 2 December 2021 at 23:29:17 UTC, Chris Katko wrote:
 there's:

 ```d
   import core.thread;
   Thread.sleep( dur!("msecs")(10) );
 ```

 but what if you want to simply yield all remaining time back to 
 the time scheduler?

 Is there a D std.library accessible version of POSIX 
 sched_yield:
There's https://dlang.org/phobos/core_thread_osthread.html#.Thread.yield
 It seems I can (thanks to the amazing work of D community) 
 simply do:

 ```d
 extern(C) int sched_yield(void);  // #include <sched.h>
 ```

 however, how does the linker know I need <sched.h> and not some 
 local library, or SDL library, or SDL2.0 library, etc. 
 Shouldn't I be specifying the library somewhere?
Linker doesn't need sched.h. Like H.S.Theoh said, it'll look for that symbol in libraries you're linking against. sched_yield is in libc, and by default you do link against that.
 ```
 source/app.d(226,16): Error: cannot have parameter of type 
 `void`
 ```

 Should that be corrected in the compiler? Shouldn't () and 
 (void) be interchangeable as long as you're not doing void*?
No: https://dlang.org/articles/ctod.html#funcvoid
Dec 02 2021
prev sibling parent Paul Backus <snarwin gmail.com> writes:
On Thursday, 2 December 2021 at 23:29:17 UTC, Chris Katko wrote:
 Is there a D std.library accessible version of POSIX 
 sched_yield:

   https://man7.org/linux/man-pages/man2/sched_yield.2.html
D bindings for POSIX headers are provided by modules in the package `core.sys.posix`. So, for `sched.h`, you would import `core.sys.posix.sched`.
Dec 04 2021