> In <pa****************************@bar.net> "Mac" <fo*@bar.net> writes: Is it legal to declare errno after you've included errno.h?
For example:
#include<errno.h>
...
int main (void) { extern int errno;
...
}
I see in the standard that errno may be a macro, and I see that defining errno is illegal, but I don't think the declaration above counts as a definition since it doesn't reserve storage. On the other hand, if errno IS a macro, the declaration above could easily be a syntax error after pre-processing.
Please enlighten me. Your analysis is correct. You cannot even declare errno, because it can be (and quite often is) defined as a macro in <errno.h>. The common reason for this is allowing multithreaded applications to have a per thread errno, rather than sharing a global errno. This is one of the few places where the C standard cares about multithreading.
[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
The common macro definition for errno is along the lines:
#define errno (*__errno())
So, if you need to access errno, include <errno.h> and use whatever definition/declaration it provides.
Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de 11 2498
In article <2i************@uni-berlin.de>,
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote: I am at loss as how defining `errno' as a macro can be helpful in multithreading applications. Please enlighten me.
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.
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> writes:
[...] I am at loss as how defining `errno' as a macro can be helpful in multithreading applications. Please enlighten me.
As Dan wrote: The common macro definition for errno is along the lines:
#define errno (*__errno())
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 (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk... In article <2i************@uni-berlin.de>, "Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote:
I am at loss as how defining `errno' as a macro can be helpful in multithreading applications. Please enlighten me.
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.
Nicely explained. Thanks.
"Keith Thompson" <ks***@mib.org> wrote in message news:ln************@nuthaus.mib.org... "Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> writes: [...] I am at loss as how defining `errno' as a macro can be helpful in multithreading applications. Please enlighten me. As Dan wrote:
The common macro definition for errno is along the lines:
#define errno (*__errno())
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.
This paragraph clears my doubt. Thanks.
-- Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.
in comp.lang.c i read: In article <2i************@uni-berlin.de>, "Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote: I am at loss as how defining `errno' as a macro can be helpful in multithreading applications. Please enlighten me.
If "errno" were a plain extern variable, then all threads in a multithreading application would always see the same "errno" variable.
this is not required or always true (think platform magic). but it's off-
topic so i'll not go into it further.
--
a signature
In <m1*************@usa.net> those who know me have no need of my name <no****************@usa.net> writes: in comp.lang.c i read:In article <2i************@uni-berlin.de>, "Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote:
I am at loss as how defining `errno' as a macro can be helpful in multithreading applications. Please enlighten me.
If "errno" were a plain extern variable, then all threads in a
^^^^^^^^^^^^^^^^^^^^^^^multithreading application would always see the same "errno" variable.
this is not required or always true (think platform magic).
Of course it is required and always true. Once implementation magic gets
involved, errno is no longer "a plain extern variable".
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In article <c9**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote: I am at loss as how defining `errno' as a macro can be helpful in multithreading applications. Please enlighten me.
If "errno" were a plain extern variable, then all threads in a ^^^^^^^^^^^^^^^^^^^^^^^multithreading application would always see the same "errno" variable.
this is not required or always true (think platform magic).
Of course it is required and always true. Once implementation magic gets involved, errno is no longer "a plain extern variable".
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
"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9**********@sunnews.cern.ch... Of course it is required and always true. Once implementation magic gets involved, errno is no longer "a plain extern variable".
I can be. On the environment I work on, it is. Its value is swapped during
a context switch along with the registers.
DrX
In <c9*********@cui1.lmms.lmco.com> "Xenos" <do**********@spamhate.com> writes: "Dan Pop" <Da*****@cern.ch> wrote in message news:c9**********@sunnews.cern.ch...
Of course it is required and always true. Once implementation magic gets involved, errno is no longer "a plain extern variable". I can be. On the environment I work on, it is. Its value is swapped during a context switch along with the registers.
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <c9***********@pc-news.cogsci.ed.ac.uk> ri*****@cogsci.ed.ac.uk (Richard Tobin) writes: In article <c9**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
> I am at loss as how defining `errno' as a macro can be helpful in > multithreading applications. Please enlighten me.If "errno" were a plain extern variable, then all threads in a ^^^^^^^^^^^^^^^^^^^^^^^multithreading application would always see the same "errno" variable.this is not required or always true (think platform magic).
Of course it is required and always true. Once implementation magic gets involved, errno is no longer "a plain extern variable".
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.
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9**********@sunnews.cern.ch... In <c9*********@cui1.lmms.lmco.com> "Xenos" <do**********@spamhate.com>
writes:
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
That's just arguing sematics. A thread switch is a context switch, though
there are other kinds of context switches.
DrX This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tak-Shing Chan |
last post by:
Dear c.l.c regulars,
How about codifying a list of acceptable acronyms on c.l.c?
<g>
<g,d&r>
<VBG>
AAMOF
AFAIAA
AFAIAC
|
by: Richard Tobin |
last post by:
In a library I am writing, I want to use an errno-like mechanism
for error returns. The error would probably be represented as a
struct rather than just an integer.
I don't have any...
|
by: Mac |
last post by:
Is it legal to declare errno after you've included errno.h?
For example:
#include<errno.h>
....
int main (void)
{
|
by: Paul Emmons |
last post by:
If I am writing a function that sets errno according to a possible
error condition, should it also set errno to 0 if it executes
normally, or should it leave errno alone on success?
|
by: Walter Roberson |
last post by:
If realloc() finds it necessary to move the memory block, then
does it free() the previously allocated block?
The C89 standard has some reference to undefined behaviour if one
realloc()'s memory...
|
by: pete |
last post by:
On my system, the following five expressions are true:
(HUGE_VAL == HUGE_VAL / 2)
(1 / HUGE_VAL == 0)
(sqrt(HUGE_VAL) == HUGE_VAL)
(sqrt(-1) == HUGE_VAL)
(sqrt(-HUGE_VAL) == HUGE_VAL)
and
|
by: Urs Beeli |
last post by:
I have a question regarding errno. If I understand it correctly,
including <errno.h> allows me to check "errno" for error values
that some standard library functions may set.
This brings up some...
|
by: jacob navia |
last post by:
???
Not that I miss his posts, but somehow I find sad that somebody
disappears from view and nobody gives a damm.
Anybody knows what happened to him?
He had lost is job, and was looking for...
|
by: Spiros Bousbouras |
last post by:
Assume I'm writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |