`errno' (Dan Pop)

Discussion in 'C Programming' started by Vijay Kumar R Zanvar, Jun 1, 2004.

  1. [Today I happened to see that this mail was hiding in the "Drafts" folder
    for more than a month!]

    I am at loss as how defining `errno' as a macro can be helpful in
    multithreading applications. Please enlighten me.
    Vijay Kumar R Zanvar, Jun 1, 2004
    1. Advertisements

  2. If "errno" were a plain extern variable, then all threads in a
    multithreading application would always see the same "errno" variable.
    So if thread A calls a function which might change errno, then checks
    errno immediately after the function call, then another thread B could
    be calling a different function modifying errno just before thread A
    reads the value of errno. So thread A might read the wrong value.

    In multithreading applications, you usually have a modified standard
    library where every thread has its own "errno" variable, and a library
    function called by thread X modifies the errno variable belonging to
    thread X. Usually errno is a macro defined as a function call returning
    the errno variable of the current thread.
    Christian Bau, Jun 1, 2004
    1. Advertisements

  3. Assume each thread has its own data area. The hypothetical __errno()
    function returns a pointer into the data area of the current thread.
    This way, each thread effectively gets its own unique errno variable.
    Keith Thompson, Jun 1, 2004
  4. Nicely explained. Thanks.
    Vijay Kumar R Zanvar, Jun 1, 2004
  5. This paragraph clears my doubt. Thanks.
    Vijay Kumar R Zanvar, Jun 1, 2004
  6. in comp.lang.c i read:
    those who know me have no need of my name, Jun 1, 2004
  7. Vijay Kumar R Zanvar

    Dan Pop Guest

    Dan Pop, Jun 2, 2004
  8. I think by "if errno were a plain extern variable" the poster meant
    "if errno's declaration were a plain extern variable declaration", in
    contrast to being declared as a macro. At least, that reading is one
    that makes his comment comprehensible.

    -- Richard
    Richard Tobin, Jun 2, 2004
  9. Vijay Kumar R Zanvar

    Xenos Guest

    I can be. On the environment I work on, it is. Its value is swapped during
    a context switch along with the registers.

    Xenos, Jun 2, 2004
  10. Vijay Kumar R Zanvar

    Dan Pop Guest

    This is another way to say that each context has its own errno, which is
    perfectly natural and doesn't require any compiler magic. However, a
    context switch and a thread switch are not the same thing.

    Dan Pop, Jun 3, 2004
  11. Vijay Kumar R Zanvar

    Dan Pop Guest

    I doubt many implementors would bother making implementation magic work
    for ordinarily declared identifiers. It's far more sensible to put the
    implementation magic in the declaration of the identifier, like:

    extern __thread_local int errno;

    in which case errno is no longer a plain extern variable.

    Dan Pop, Jun 3, 2004
  12. Vijay Kumar R Zanvar

    Xenos Guest

    That's just arguing sematics. A thread switch is a context switch, though
    there are other kinds of context switches.

    Xenos, Jun 3, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.