1

Sorry for the long title, but I think it explains well what I'm interested in. For example C function strtok is not thread safe in worst possible way :) , it uses a global state. So even if it is called on different data it is not thread safe. So my question is are there functions in "C++ minus C" that have same problem. Again I'm not interested in things like "if you write to same file from 10 threads it is undefined behavior". What I'm interested is "if you write to 2 diff files from 2 diff threads (each thread writes to its own file )that is not thread safe."

NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • 1
    To clarify, do you mean anything in the C++ standard library that does *not* come from the included C standard library? Maybe something in `` uses a global state... – Kerrek SB Sep 19 '11 at 13:18
  • 3
    I don't think the C++ standard makes any guarantees at all about how things shared between threads will behave (other than the new atomic/mutex stuff, obviously). So nothing is safe, unless you explicitly use synchronization constructs, or a library implementation that explicitly guarantees thread-safe constructs. – Oliver Charlesworth Sep 19 '11 at 13:20
  • What about `rand()`/`srand()`? – sharptooth Sep 19 '11 at 13:22
  • Oh, I see the distinction you're making. You're talking about things which "should" be thread-safe, but aren't. – Oliver Charlesworth Sep 19 '11 at 13:26
  • 1
    @sharptooth: There are plenty of examples in the C library that come in an unsafe and a reentrant version, e.g. `drand48_r()`, `mbsrtowcs()`, `ctime_r()` (though some of those are extensions). I suppose the OP wants to know about C++ constructs that are *not* in the C library. – Kerrek SB Sep 19 '11 at 13:32
  • @Oli - well then we can talk about popular implementations. I mean GCC, MSVC, Clang... I mean if for example you could only write to one file at the time or if you couldnt run 2 string constructors at the same time it would be hell. – NoSenseEtAl Sep 19 '11 at 13:43
  • @KerrekSB mostly I'm interested in newer CPP stuff, STL, iostreams... – NoSenseEtAl Sep 19 '11 at 13:44
  • 1
    @Oli: C++11 certainly does; see "data race" – MSalters Sep 19 '11 at 13:59

3 Answers3

3

Thread safety is only really covered by C++11; C++03 didn't specify multi-threaded behavior.

In C++11, the relevant bits are 1.10/8 "Certain library calls synchronize with other library calls performed by another thread. For example, an atomic store-release synchronizes with a load-acquire that takes its value from the store (29.3)." and especially §17.6.5.9 Data race avoidance.

The cases you mention are obviously disallowed: "

  1. This section specifies requirements that implementations shall meet to prevent data races (1.10). Every standard library function shall meet each requirement unless otherwise specified. Implementations may prevent data races in cases other than those specified below.
  2. A C++ standard library function shall not directly or indirectly access objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s arguments, including this.
  3. A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s nonconst arguments, including this.
  4. [ Note: This means, for example, that implementations can’t use a static object for internal purposes without synchronization because it could cause a data race even in programs that do not explicitly share objects between threads. —end note ]

Where the text above says "unless otherwise specified", it includes some C function, e.g. (27.9.2/2) "Calls to the function tmpnam with an argument of NULL may introduce a data race (17.6.5.9) with other calls to tmpnam with an argument of NULL."

MSalters
  • 173,980
  • 10
  • 155
  • 350
2

C++ standard doesn't guarantee new (or malloc()) to be thread-safe. Even though it's very crucial to have them thread-safe.

However, most platforms support thread-safe new.

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
1

Actually, C++ differentiates between "read-only thread safety" and "full thread safety". E.g. all std containers are "read-only" thread-safe and not safe if any of the threads modifies the container.

However, when you do not access shared data, I don't recall any of C++ features being unsafe.

Lyth
  • 2,171
  • 2
  • 29
  • 37
  • @sharptooth afaik, `rand`/`srand` use thread-local storage, but I'm not quite sure about that. – Lyth Sep 19 '11 at 13:31