Is atoi multithread safe? [closed]

Is atoi multithread safe?

Yes, in the linux man page of atoi() it is written:

┌────────────────────────┬───────────────┬────────────────┐
│Interface               │ Attribute     │ Value          │
├────────────────────────┼───────────────┼────────────────┤
│atoi(), atol(), atoll() │ Thread safety │ MT-Safe locale │
└────────────────────────┴───────────────┴────────────────┘

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 argument const char* nptr. It is a pointer to a constant char array.


Here is also an explanation of the terms/attributes.

MT-Safe:

MT-Safe or Thread-Safe functions are safe to call in the presence of other threads. MT, in MT-Safe, stands for Multi Thread.

locale:

locale Functions annotated with locale as an MT-Safety issue read
from the locale object without any form of synchronization.
Functions annotated with locale called concurrently with
locale changes may behave in ways that do not correspond to
any of the locales active during their execution, but an
unpredictable mix thereof.


While using gdb to debug, the atoi function is throwing error.

The atoi() function doesn’t provide any error information at all, if the conversion is not successful it returns 0 and you don’t know if that may be the actual number to convert. Further the atoi() function does not throw at all! The following output I produced with a little portion of C code, see online at ideone:

atoi with "3"        to integer: +3
atoi with "    3   " to integer: +3
atoi with "   -3   " to integer: -3
atoi with "str 3   " to integer: +0
atoi with "str-3   " to integer: +0
atoi with "    3str" to integer: +3
atoi with "   -3str" to integer: -3
atoi with "str-3str" to integer: +0

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 and return 0 and does not throw.


You should consider using strtol() instead as it can detect range overflows in which case is sets the errno.
Further you get an end pointer which show you how much characters were consumed. If that value is 0 there must be something wrong with the conversion. It is threadsafe like atoi().

I did the same to output it for strtol(), you can see it also in the ideone online example from above:

0: strtol with "3"         to integer: +3 | errno =  0, StartPtr = 0x7ffc47e9a140, EndPtr = 0x7ffc47e9a141, PtrDiff = 1
1: strtol with "    3   "  to integer: +3 | errno =  0, StartPtr = 0x7ffc47e9a130, EndPtr = 0x7ffc47e9a135, PtrDiff = 5
2: strtol with "   -3   "  to integer: -3 | errno =  0, StartPtr = 0x7ffc47e9a120, EndPtr = 0x7ffc47e9a125, PtrDiff = 5
3: strtol with "str 3   "  to integer: +0 | errno =  0, StartPtr = 0x7ffc47e9a110, EndPtr = 0x7ffc47e9a110, PtrDiff = 0 --> Error!
4: strtol with "str-3   "  to integer: +0 | errno =  0, StartPtr = 0x7ffc47e9a100, EndPtr = 0x7ffc47e9a100, PtrDiff = 0 --> Error!
5: strtol with "    3str"  to integer: +3 | errno =  0, StartPtr = 0x7ffc47e9a0f0, EndPtr = 0x7ffc47e9a0f5, PtrDiff = 5
6: strtol with "   -3str"  to integer: -3 | errno =  0, StartPtr = 0x7ffc47e9a0e0, EndPtr = 0x7ffc47e9a0e5, PtrDiff = 5
7: strtol with "str-3str"  to integer: +0 | errno =  0, StartPtr = 0x7ffc47e9a0d0, EndPtr = 0x7ffc47e9a0d0, PtrDiff = 0 --> Error!
8: strtol with "s-r-3str"  to integer: +0 | errno =  0, StartPtr = 0x7ffc47e9a0c0, EndPtr = 0x7ffc47e9a0c0, PtrDiff = 0 --> Error!

On this thread: Detecting strtol failure the right usage of strtol() is discussed concerning error detection.

Leave a Comment