> 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 2527
In article <2i************ @uni-berlin.de>,
"Vijay Kumar R Zanvar" <vi*****@global edgesoft.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*****@global edgesoft.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_Keit h) 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*****@global edgesoft.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.or g> wrote in message news:ln******** ****@nuthaus.mi b.org... "Vijay Kumar R Zanvar" <vi*****@global edgesoft.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_Keit h) 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*****@global edgesoft.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 multithreadi ng 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*****@global edgesoft.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
^^^^^^^^^^^^^^^ ^^^^^^^^multithreadin g 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**********@s unnews.cern.ch> , Dan Pop <Da*****@cern.c h> 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 ^^^^^^^^^^^^^^^ ^^^^^^^^multithreadi ng 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.c h> 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*********@cu i1.lmms.lmco.co m> "Xenos" <do**********@s pamhate.com> writes: "Dan Pop" <Da*****@cern.c h> wrote in message news:c9******* ***@sunnews.cer n.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 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 multi-threaded programs, but others may who want to
use my library. Most likely they'll be using a pthreads-compatible
threads system.
How can I best accommodate them? Presumably they will want the error
object to be in per-thread storage, so to set and...
|
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 that was freed by realloc(), but the only way
explicitly mentioned in the C89 standard to free memory via realloc()
is to realloc() it down to 0 bytes.
I had always assumed it would automatically free the previous memory,
but is the behaviour...
| |
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 questions:
- As I understand it, most functions only set errno in an error
case. I take this to mean, that on success, errno is not
necessarily set to EOK and I would either need to set errno
to EOK before calling the function in question to...
|
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 a new one last time he appeared
here.
|
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 succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;
Is this ok ?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |