MySQL Forums
Forum List  »  Connector/C++

MySQL C++ Connector and thread detached
Posted by: Adrian Vlad
Date: June 03, 2015 10:03AM

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

Options: ReplyQuote


Subject
Views
Written By
Posted
MySQL C++ Connector and thread detached
2649
June 03, 2015 10:03AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.