I have a vector (a large buffer of strings) which I hope to read from a network thread while it is being written to by the main thread.
As vector
is not thread-safe, the typical approach would be to lock the access to it from both threads.
However my insight has been that this should be only strictly necessary when the particular write will resize the vector, which will cause the realloc, which will cause any concurrent readers to have their carpet yanked. At all other times, provided that the concurrent reader is not reading the entry currently being written to, reading and writing to the vector should be allowed to happen concurrently.
So, I'd want to (somehow) only lock the mutex from the write thread when I know that the write I am about to do will cause a resize operation that will realloc the vector.
Is this practical? Can I deduce based on the value returned by capacity
and the current size, for certain whether or not the next push_back
will resize?