I am experiancing some error while creating a multithreaded program. While using gdb to debug, the atoi function is throwing error. Please help, is atoi multithread unsafe and if so, what are the alternatives?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- How to let a thread communicate with another activ
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
Yes, in the linux man page of atoi() it is written:
So it's just using the variables you pass from your thread (locale) and is completely thread-safe (MT-Safe), as long as you don't pass the same memory location e.g. a pointer to a char array from two threads to that function.
If you would do that, both funcions calls (thread one and two) would use the same memory location, in the case of
atoi()
it is not that bad, because that function only reads from memory, see the argumentconst char* nptr
. It is a pointer to a constant char array.Here is also an explanation of the terms/attributes.
MT-Safe:
locale:
The
atoi()
function doesn't provide any error information at all, if the conversion is not successful it returns0
and you don't know if that may be the actual number to convert. Further theatoi()
function does not throw at all! The following output I produced with a little portion of C code, see online at ideone:You can see that
atoi()
converts successfully if the first part is a number ignoring whitespace and characters after the first number part. If there are non numerical characters first it fails andreturn 0
and does not throw.You should consider using
strtol()
instead as it can detect range overflows in which case is sets theerrno
.Further you get an
end pointer
which show you how much characters were consumed. If that value is0
there must be something wrong with the conversion. It is threadsafe likeatoi()
.I did the same to output it for
strtol()
, you can see it also in the ideone online example from above:On this thread: Detecting strtol failure the right usage of
strtol()
is discussed concerning error detection.Its quite easy to implement a replacement for
atoi()
:(Demonstration on ideone)
It doesn't seem to make much sense to replace something which is already available. Thus, I want to mention some thouhgts about this.
The implementation can be adjusted to the precise personal requirements:
text
may be returned (as instrtol()
) to check how many characters have been processed or to do further parsing of other contentsunsigned
(which does not accept a sign).Extending this idea to other numeric types like e.g.
float
ordouble
, it becomes even more interesting.As floating point numbers are definitely subject of localization this has to be considered. (Concerning decimal integer numbers I'm not sure what could be localized but even this might be the case.) If a text file reader with floating point number syntax (like in C) is implemented you may not forget to adjust the locale to
C
before usingstrtod()
(usingsetlocale()
). (Being a German I'm sensitive to this topic, as in the German locale, the meaning of '.' and ',' are just vice versa like in English.)Another fact is, that consideration of locale (even if adjusted to C) seems to be somehow expensive. Some years ago, we implemented an own floating point reader as replacement of
strtod()
which provided a speed-up of 60 ... 100 in a COLLADA reader (an XML file format where files often provide lots of floating point numbers).Update:
Encouraged by the feedback of Paul Floyd, I got curious how faster
strToInt()
might be. Thus, I built a simple test suite and made some measurements:I tried this on some platforms.
VS2013 on Windows 10 (64 bit), Release mode:
gcc 5.4.0 on cygwin, Windows 10 (64 bit),
gcc -std=c11 -O2
:Sample uploaded and executed on codingground
gcc 4.8.5 on Linux 3.10.0-327.36.3.el7.x86_64,
gcc -std=c11 -O2
:Well,
strToInt()
is a little bit faster. (Without-O2
, it was even slower thanatoi()
but the standard library was probably optimized too.)Note:
As the time measurement involves assignment and loop operations, this provides a qualitative statement about which one is faster. It doesn't provide a quantitative factor. (To get one, the measurement would become much more complicated.)
Due to the simplicity of
atoi()
, the application had to use it very often until it becomes even worth to consider the development effort...