...
options["OPT_WRITE_TIMEOUT"] = timeout;
std::lock_guard<std::recursive_mutex> locker(mutex_);
auto driver = sql::mysql::get_driver_instance();
connection_.reset(driver->connect(options));
...
This code is being executed in a single thread. It's like a connection thread.
All what I want is to halt this connection during the process of establishing a connection to start a new connection, say, with new changed options. Mustn't there be a safe way to do that or I'm doomed to wait until the current connection attempt has exceeded the timeout?
So, the documentation of get_driver_instance()
says that you must not simultaneously invoke it from multiple threads. I suppose that's because the first time it is invoked it will create the driver, so re-entrance of that method may result in the creation of multiple instances of the driver. That's all very fine, because you can invoke this method during the static initialization of your program, outside of any spawned threads, so there is no need to guard it with a mutex. So, you obtain the driver once and store it for use by the rest of your program.
The documentation of driver->connect()
does not say that it is not re-entrant, so it probably is re-entrant. This means that you can spawn one thread to try and connect, and spawn additional threads which also try to connect with different parameters.
So, you should not need a mutex anywhere. If for some reason you really don't want to obtain the driver once and store it for later use, then you can make use of the mutex to guard against re-entering the call to get_driver_instance()
, but you do not (and should not) use the mutex to guard against re-entering the call to driver->connect()
. Thus, you will be able to invoke driver->connect()
in parallel from multiple threads.
Incidentally, get_driver_instance()
should return immediately; it is the driver->connect()
call which may take a long time.