sleeping vs sched_yield

H. S. Teoh hsteoh at
Thu Dec 2 23:40:06 UTC 2021

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

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)`.


There are four kinds of lies: lies, damn lies, and statistics.

More information about the Digitalmars-d-learn mailing list