473,320 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

errno-like variables in threaded environments

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 retrieve it I should
provide functions or macros they can redefine. Ideally I'd like to be
able to build a single library (for each platform, using whatever
thread library is commonest there) that works with both threaded and
unthreaded applications, but I doubt that's possible.

-- Richard
--
Spam filter: to mail me from a .com/.net site, put my surname in the headers.

FreeBSD rules!
Nov 14 '05 #1
4 1526

I did some research on this "threads and errno" subject a while ago and
found this usenet thread (link might wrap a bit)

David Butenhorf's wrote in that thread for instance:

"As long as code doesn't
use the errno token in a manner that violates ANSI C, the thread-specific
errno token is transparent to the code."

here's the whole post (by David Butenhorf) and link to the thread too:
I think 'errno' is thread-safe. Just wonder that is it a standard?
What standard is it?
That depends on what threads you're using. If you're using POSIX threads
("pthread" prefixes), the interfaces are specified by the POSIX 1003.1-1996
(ISO/IEC 9945-1:1996) international standard. POSIX requires that each
thread must have a unique private errno value.

POSIX doesn't specify how this is to be accomplished, but the most common
mechanism is for the <errno.h> header, when compiled with proper thread
options (generally -D_REENTRANT), to define the token "errno" as a macro
that expands to something like "*(_errno())", where _errno() is a function
returning "int *". This allows you to use "if (errno)", or "x = errno", or
even "errno = x" just as you would normally.

Note that ANSI C 89 has always required that any code using the errno token
must have a #include <errno.h> in scope. POSIX 1003.1-1990 (an earlier
version of the POSIX standard that didn't include threads) "overrode" that
requirement, specifying that "traditional C" practice of using the errno
token with only "extern int errno;" in scope. POSIX 1003.1-1996 removed the
earlier override, falling back to the ANSI C rules. As long as code doesn't
use the errno token in a manner that violates ANSI C, the thread-specific
errno token is transparent to the code.
http://groups.google.fi/groups?hl=fi...22%2Bwindows%2
6hl%3Dfi%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D9gr1f5%2524alj%25241%254
0nntp9.atl.mindspring.net%26rnum%3D2&seekm=9gr1f5% 24alj%241%40nntp9.atl.mind
spring.net

"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
news:c1***********@pc-news.cogsci.ed.ac.uk... 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 retrieve it I should
provide functions or macros they can redefine. Ideally I'd like to be
able to build a single library (for each platform, using whatever
thread library is commonest there) that works with both threaded and
unthreaded applications, but I doubt that's possible.

-- Richard
--
Spam filter: to mail me from a .com/.net site, put my surname in the headers.
FreeBSD rules!


with respect,
Toni Uusitalo
Nov 14 '05 #2

"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
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.

You've got huge problems. ANSI C doesn't define any threading behaviour, but
typically threads will have their own stacks and share globals. What this
means is that it is very difficult to prevent the wrong thread getting hold
of an error set by another. What's worse is that the conflict won't happen
very often, so the program will be fine when you test it and then the bug
will show up just when you're least ready to deal with it, for example the
day before you ship.

One way is to pass a parameter to receive error data. This will uglify your
code but has the advantage of reminding the caller to check for failure.
Another way is to use another language that has error-handling mechanisms.
This is rather an extreme step to take.
Nov 14 '05 #3
Toni Uusitalo wrote:

I did some research on this "threads and errno"
subject a while ago and
found this usenet thread (link might wrap a bit)


I think that the most important thing mentioned here,
is that there is such a newsgroup as:
news:comp.programming.threads

--
pete
Nov 14 '05 #4
In article <40***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
I think that the most important thing mentioned here,
is that there is such a newsgroup as:
news:comp.programming.threads


Good point, I'll post a copy there.

-- Richard
--
Spam filter: to mail me from a .com/.net site, put my surname in the headers.

FreeBSD rules!
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alex Lapp | last post by:
Hi ng, i have a problem installing cvs module from: http://www.object-craft.com.au/projects/csv/ My Python version is: Python 2.1.3 (Not the latest, but i need this version for running Zope...
3
by: unspammable | last post by:
How did the errno variable get its name? I always figured it's short for "error number", but then it would have been better to call it errnum... no? (Come to think of it, this is probably a C...
3
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) {
2
by: RoSsIaCrIiLoIA | last post by:
Is it good the use of va_arg? ____________________ #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <errno.h> unsigned gcd_my(unsigned a, unsigned b) {unsigned t;
4
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?
11
by: Vijay Kumar R Zanvar | last post by:
> In <pan.2004.04.22.04.06.05.969827@bar.net> "Mac" <foo@bar.net> writes: > > >Is it legal to declare errno after you've included errno.h? > > > >For example: > > > >#include<errno.h> > > >...
6
by: Michael McGarry | last post by:
Hi, How do I interpret errno from fopen()? errno = 24 in my case. Thanks for any help, Michael
3
by: eyalc1978 | last post by:
Hi Does someone knows what does errno 72 means I got this error when fwriting a structure and still the file is being created. I saw something on the net saying that this is caused when trying to...
5
by: Gregory Piñero | last post by:
Hi Guys, I'm sure this is documented somewhere, I just can't locate it. Say I have this code: try: myfile=file('greg.txt','r') except IOError, error: #now psuedo code because this is what...
22
by: viza | last post by:
Hi all, A quick one - since errno is a lvalue, can I do: fread( & errno, sizeof errno, 1, fp ) ? TIA viza
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.