Hello,
I've recently started to use MySQL C++ Connector for a multithreaded application.
The scenario is pretty simple: at a given time a connection will be made and passed to a newly created thread that will make some database operations using that connection and the thread will exit. I verified my application for races and other multithreading problems using valgrind with helgrind tool and I saw that were reported a lot of possible races.
After much digging I found that the problem occurs when the thread is not joined. In my application, the driver is initialized once with get_driver_instance() and used to create the connections. The created connection is passed to the thread. The thread is created and then detached (pthread_detach()). It does it's job and deletes the connection.
I modified the example pthreads.cpp to not wait for the thread via join(), but to detach the thread and the waiting will still be made by a while loop which checks a thread_finished variable that is set to 1 at the end of thread execution.
// status = pthread_join(thread_one, NULL);
// if (status != 0)
// throw std::runtime_error("Joining thread has failed");
pthread_detach(thread_one);
This modification triggers the same race problems when the example is analized with helgrind.
My question is why the races are still present and how can I fix them?
I don't want to use thread join because of the design of the application (it just spawns the threads at specific times and lets them do their job; any error will be logged and analized later).
Thank you,
Adrian
I've recently started to use MySQL C++ Connector for a multithreaded application.
The scenario is pretty simple: at a given time a connection will be made and passed to a newly created thread that will make some database operations using that connection and the thread will exit. I verified my application for races and other multithreading problems using valgrind with helgrind tool and I saw that were reported a lot of possible races.
After much digging I found that the problem occurs when the thread is not joined. In my application, the driver is initialized once with get_driver_instance() and used to create the connections. The created connection is passed to the thread. The thread is created and then detached (pthread_detach()). It does it's job and deletes the connection.
I modified the example pthreads.cpp to not wait for the thread via join(), but to detach the thread and the waiting will still be made by a while loop which checks a thread_finished variable that is set to 1 at the end of thread execution.
// status = pthread_join(thread_one, NULL);
// if (status != 0)
// throw std::runtime_error("Joining thread has failed");
pthread_detach(thread_one);
This modification triggers the same race problems when the example is analized with helgrind.
My question is why the races are still present and how can I fix them?
I don't want to use thread join because of the design of the application (it just spawns the threads at specific times and lets them do their job; any error will be logged and analized later).
Thank you,
Adrian