List:MySQL++« Previous MessageNext Message »
From:Damien Kick Date:July 11 2014 8:41pm
Subject:Re: Problem with attempt to share a Connection between threads
View as plain text  
Apple's Mail seems to keep stripping quotes.  Sorry if it does it again.

On Jul 11, 2014, at 3:05 PM, Warren Young
<mysqlpp@stripped<mailto:mysqlpp@stripped>>
 wrote:

On 7/11/2014 13:44, Damien Kick wrote:

$ ./zeros.og
Segmentation fault: 11 (core dumped)
$ ./zeros.og
Exec time (in seconds): 18.1346
$ ./zeros.og
Segmentation fault: 11 (core dumped)
$ ./zeros.og
Segmentation fault: 11 (core dumped)
$ ./zeros.og
Exec time (in seconds): 19.5771
$ gdb zeros.og

Classic symptom of a race condition.

For what it's worth, ensuring that I am only creating one Connection at a time is making a
difference; i.e. no more crashes.

$ cat zeros.hh
#ifndef INCLUDE_MY_ZEROS_HH
#define INCLUDE_MY_ZEROS_HH

#include <mysql++/mysql++.h>
#include <boost/thread.hpp>

namespace my {
struct Thread_guard {
    Thread_guard() { mysqlpp::Connection::thread_start(); }
    ~Thread_guard() { mysqlpp::Connection::thread_end(); }
};

#if 0
void work(boost::mutex& cxn_mutex, mysqlpp::Connection& cxn);
#endif
#if 1
void work(
    boost::mutex& cxn_mutex, const std::string& db_name,
    const std::string& server, const std::string& username,
    const std::string& password);
#endif

} // namespace my

#endif
$ cat zeros.cc
#include "zeros.hh"
#include <mysql++/mysql++.h>
#include <boost/thread.hpp>
#include <string>

int main()
{
    boost::thread_group threads;
    boost::mutex cxn_mutex;
    auto lambda = [&cxn_mutex] () {
        my::work(cxn_mutex, "…", "…", "…", "…");
    };

    const auto start_time = boost::chrono::steady_clock::now();

    for (auto n = 0; n < 8; ++n) {
        threads.create_thread(lambda);
    }
    threads.join_all();

    const auto end_time = boost::chrono::steady_clock::now();
    const auto elapsed = end_time - start_time;
    typedef boost::chrono::duration<float> Seconds;
    const auto seconds = boost::chrono::duration_cast<Seconds>(elapsed);
    std::cout << "Exec time (in seconds): " << seconds.count() << '\n';
}

void my::work(
    boost::mutex& cxn_mutex,
    const std::string& db_name,
    const std::string& server,
    const std::string& username,
    const std::string& password)
{
    boost::unique_lock<boost::mutex> cxn_lock(cxn_mutex);
    mysqlpp::Connection cxn(
        db_name.c_str(), server.c_str(), username.c_str(),
        password.c_str());
    cxn_lock.unlock();
    for (auto n = 0; n < 100; ++n) {
        auto query = cxn.query("SET @zero = 0");
        query.exec();
    }
}
$ for i in {1..100}; do ./zeros; done
Exec time (in seconds): 42.9176
Exec time (in seconds): 43.5692
Exec time (in seconds): 29.7254
Exec time (in seconds): 24.3269
Exec time (in seconds): 23.5109
Exec time (in seconds): 23.8989
Exec time (in seconds): 20.3862
Exec time (in seconds): 22.0921
Exec time (in seconds): 24.4794
Exec time (in seconds): 23.5504
Exec time (in seconds): 26.1323
Exec time (in seconds): 50.6923
Exec time (in seconds): 34.0941
Exec time (in seconds): 38.2812
Exec time (in seconds): 24.4601
Exec time (in seconds): 24.332

I realize that this isn't definitive, but it makes me suspect some kind of race in the
internals of Connection.

What happens when you run it under valgrind --tool=helgrind?

   http://valgrind.org/docs/manual/hg-manual.html

A regular Valgrind run (memcheck) may also be enlightening.

Unfortunately, valgrind doesn't work reliably on a Mac yet, at least not according to the
Fink port I've got.

$ valgrind ls
==22176== Memcheck, a memory error detector
==22176== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==22176== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==22176== Command: ls
==22176==
==22176== WARNING: Support on MacOS 10.8 is experimental and mostly broken.
==22176== WARNING: Expect incorrect results, assertions and crashes.
==22176== WARNING: In particular, Memcheck on 32-bit programs will fail to
==22176== WARNING: detect any errors associated with heap-allocated data.
==22176==

I will try again on a GNU/Linux box when I have more time and/or easier access to another
box.

________________________________

This e-mail and any attachments are confidential. If it is not intended for you, please
notify the sender, and please erase and ignore the contents.

Thread
Problem with attempt to share a Connection between threadsDamien Kick10 Jul 2014
  • Re: Problem with attempt to share a Connection between threadsWarren Young11 Jul 2014
    • Re: Problem with attempt to share a Connection between threadsDamien Kick11 Jul 2014
      • Re: Problem with attempt to share a Connection between threadsWarren Young11 Jul 2014
        • Re: Problem with attempt to share a Connection between threadsDamien Kick11 Jul 2014
          • Re: Problem with attempt to share a Connection between threadsWarren Young11 Jul 2014
            • Re: Problem with attempt to share a Connection between threadsDamien Kick11 Jul 2014
              • Re: Problem with attempt to share a Connection between threadsDamien Kick11 Jul 2014
                • Re: Problem with attempt to share a Connection between threadsWarren Young11 Jul 2014
              • Re: Problem with attempt to share a Connection between threadsWarren Young11 Jul 2014
                • Re: Problem with attempt to share a Connection between threadsDamien Kick11 Jul 2014
                  • Re: Problem with attempt to share a Connection between threadsWarren Young11 Jul 2014
                    • Re: Problem with attempt to share a Connection between threadsDamien Kick11 Jul 2014
                      • Re: Problem with attempt to share a Connection between threadsAdrian Cornish11 Jul 2014
  • Re: Problem with attempt to share a Connection between threadsChris Frey13 Jul 2014
Re: Problem with attempt to share a Connection between threadsDamien Kick11 Jul 2014
  • Re: Problem with attempt to share a Connection between threadsWarren Young11 Jul 2014