List:MySQL++« Previous MessageNext Message »
From:Damien Kick Date:July 11 2014 8:03pm
Subject:Re: Problem with attempt to share a Connection between threads
View as plain text  
On Jul 11, 2014, at 2:44 PM, Damien Kick
<dkick@stripped<mailto:dkick@stripped>>
 wrote:

[…] I had been assuming that in this case (Connection per thread) there was/is no
need for mysqlpp::Connection::thread_start(); i.e., the manual states that "you
don’t need to call it when you use the simpleConnection-per-thread strategy,
because this function is implicitly called the first time you create a Connection in a
thread." However, I had found <http://lists.mysql.com/bugs/15740> which mentioned
"My apologies, I haven't tested yet, but I am pretty sure it is because I forgot to use
mysql_thread_init()/mysql_thread_end()," so I was not sure if I was understanding
correctly.

Yeah, and adding a call to mysqlpp::Connection::thread_start() even though it seems to
contradict my current understanding of the tutorial doesn't help, either.  So … I
don't know <shrug/>

$ cat zeros.hh
#ifndef INCLUDE_MY_ZEROS_HH
#define INCLUDE_MY_ZEROS_HH

#include <mysql++/mysql++.h>

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

void work(
    const std::string& db_name, const std::string& server,
    const std::string& username, const std::string& password);

} // namespace my

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

int main()
{
    boost::thread_group threads;
    auto lambda = [] () {
        my::work("shorewaremonitoring", "10.161.1.161:4310",
                 "shoreadmin", "passwordshoreadmin");
    };

    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(
    const std::string& db_name,
    const std::string& server,
    const std::string& username,
    const std::string& password)
{
    my::Thread_guard thread_guard;
    mysqlpp::Connection cxn(
        db_name.c_str(), server.c_str(), username.c_str(),
        password.c_str());
    for (auto n = 0; n < 100; ++n) {
        auto query = cxn.query("SET @zero = 0");
        query.exec();
    }
}
$ for i in {1..100}; do ./zeros.og; done
Exec time (in seconds): 20.3594
Exec time (in seconds): 16.2818
Segmentation fault: 11 (core dumped)
Exec time (in seconds): 15.0347
Exec time (in seconds): 15.0524
Exec time (in seconds): 13.9432
Exec time (in seconds): 13.5416
Exec time (in seconds): 16.6087
Exec time (in seconds): 15.2479
Segmentation fault: 11 (core dumped)
Segmentation fault: 11 (core dumped)
Segmentation fault: 11 (core dumped)
Segmentation fault: 11 (core dumped)
Exec time (in seconds): 15.9224



________________________________

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