This std::mutex
is local to the method. This means every invocation of this method has it's own mutex and doesn't protect anything.
To fix this, you must move the mutex to the same scope as the vector to_send_queue
and use a std::lock_guard
. At the website, there is an example how to use this
int g_i = 0;
std::mutex g_i_mutex; // protects g_i
void safe_increment()
{
std::lock_guard<std::mutex> lock(g_i_mutex);
++g_i;
std::cout << std::this_thread::get_id() << ": " << g_i << '\n';
// g_i_mutex is automatically released when lock
// goes out of scope
}