Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is read-only access to a vector (vector::operator[] and vector::size()) asynchronous-safe?

My program needs to perform read-only access to the contents of a vector<string> in a signal handler for SIGINT. (The alternative is to use a fixed-size array of fixed-length C strings.) The program is designed to run in a POSIX environment.

Are vector::operator[] and vector::size() asynchronous-safe (or signal-safe)?

like image 282
bwDraco Avatar asked Jan 09 '23 16:01

bwDraco


1 Answers

No, it's not safe. C++11 1.9/6:

When the processing of the abstract machine is interrupted by receipt of a signal, the values of objects which are neither

  • of type volatile std::sig_atomic_t nor
  • lock-free atomic objects (29.4)

are unspecified during the execution of the signal handler, and the value of any object not in either of these two categories that is modified by the handler becomes undefined.

like image 82
Angew is no longer proud of SO Avatar answered Jan 18 '23 22:01

Angew is no longer proud of SO