Saturday 15 September 2012

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



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

my programme needs perform read-only access contents of vector<string> in signal handler sigint. (the alternative utilize fixed-size array of fixed-length c strings.) programme designed run in posix environment.

are vector::operator[] , vector::size() asynchronous-safe (or signal-safe)?

angew's reply right considering c++. question mentions posix environment, provide stronger guarantees, needs answer, is:

if process multi-threaded, or if process single-threaded , signal handler executed other result of:

the process calling abort(), raise(), kill(), pthread_kill(), or sigqueue() generate signal not blocked

a pending signal beingness unblocked , beingness delivered before phone call unblocked returns

the behavior undefined if signal handler refers object other errno static storage duration other assigning value object declared volatile sig_atomic_t, or if signal handler calls function defined in standard other 1 of functions listed in next table.

source: open grouping base of operations specifications issue 7 ieee std 1003.1, 2013 edition, 2.4.3

this is... still weak guarantee. far can understand this:

vector::operator[] not safe. fixed arrays not safe. access fixed arrays safe if array non-static.

why? vector::operator[] doesn't specify how should implemented, preconditions , postconditions. access elements of array possible (if array non-static), implies access vector elements safe if create pointer (with vec.data() or &vec[0]) before signalling, , accessing elements through pointer.

edit: missed because wasn't aware of sigaction function - signal access local arrays in signal handler, sigaction can provide pointers automatic , dynamically arrays. advice doing little possible in signal handlers still applies here though.

bottom line: you're doing much in signal handlers. seek doing little possible. 1 approach assign flag (of type volatile sig_atomic_t), , return. code can later check if flag triggered (e.g. in event loop)

c++ vector posix signal-handling async-safe

No comments:

Post a Comment