List:MySQL++« Previous MessageNext Message »
From:Damien Kick Date:July 10 2014 6:57pm
Subject:Problem with attempt to share a Connection between threads
View as plain text  
Thanks in advance for any help.  I'm running a simple example of sharing a Connection
between multiple threads that is giving me errors:

$ ./zeros
zeros(11047,0x108268000) malloc: *** error for object 0x7f88fc008c00: pointer being freed
was not allocated
*** set a breakpoint in malloc_error_break to debug
zeros(11047,0x10836e000) malloc: *** error for object 0x7f88fbc05040: pointer being freed
was not allocated
*** set a breakpoint in malloc_error_break to debug
zeros(11047,0x10805c000) malloc: *** error for object 0x7f88fc008c00: pointer being freed
was not allocated
*** set a breakpoint in malloc_error_break to debug
zeros(11047,0x1082eb000) malloc: *** error for object 0x7f88fc008c00: double free
*** set a breakpoint in malloc_error_break to debug
zeros(11047,0x1083f1000) malloc: *** error for object 0x7f88fc008c00: double free
*** set a breakpoint in malloc_error_break to debug
zeros(11047,0x1080df000) malloc: *** error for object 0x7f88fc008c00: double free
*** set a breakpoint in malloc_error_break to debug
Segmentation fault: 11 (core dumped)
$

Details follow: am I doing anything obviously wrong?

$ uname -a
Darwin Damien-Kicks-MacBook-Pro.local 12.5.0 Darwin Kernel Version 12.5.0: Sun Sep 29
13:33:47 PDT 2013; root:xnu-2050.48.12~1/RELEASE_X86_64 x86_64
$ cat ~/tmp/dbench/src/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(mysqlpp::Connection& cxn);

} // namespace my

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

int main()
{
    my::Thread_guard thread_guard;
    assert(mysqlpp::Connection::thread_aware());
    boost::thread_group threads;
    const auto start_time = boost::chrono::steady_clock::now();

    mysqlpp::Connection cxn("…", "…", "…", "…");
    assert(cxn.thread_aware());
    assert(mysqlpp::Connection::thread_aware());
    auto lambda = [&cxn] () {
        my::work(cxn);
    };

    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 << "Elapsed time (in seconds): " << seconds.count() <<
'\n';
}

void my::work(
    mysqlpp::Connection& cxn)
{
    my::Thread_guard thread_guard;
    assert(cxn.thread_aware());
    assert(mysqlpp::Connection::thread_aware());
    for (auto n = 0; n < 100; ++n) {
        auto query = cxn.query("SET @ignore_me = 0");
        query.exec();
    }
}
$ make VERBOSE=1 zeros
…
[100%] Building CXX object CMakeFiles/zeros.dir/zeros.cc.o
g++   -DBOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS … --std=c++0x  -g
-I…/include    -o CMakeFiles/zeros.dir/zeros.cc.o -c
/Users/dkick/tmp/dbench/src/zeros.cc
Linking CXX executable zeros
/sw/bin/cmake -E cmake_link_script CMakeFiles/zeros.dir/link.txt --verbose=1
g++   --std=c++0x  -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names  
CMakeFiles/zeros.dir/zeros.cc.o  -o zeros  -L…/lib 
…/lib/libboost_chrono.dylib …/lib/libboost_system.dylib
…/lib/libboost_thread.dylib -lmysqlclient_r -lmysqlpp -Wl,-rpath,…/lib
…
$


I went back and verified that I built MySQL++ with the following configure options

$ history | grep 532 | grep -v grep
  532  ./configure --enable-thread-check --with-mysql=… --prefix=…
$

And that libmysqlpp was made using the reentrant version of the library.

g++ -dynamiclib -single_module -headerpad_max_install_names -o libmysqlpp.3.2.1.dylib
mysqlpp_beemutex.o mysqlpp_cmdline.o mysqlpp_connection.o mysqlpp_cpool.o
mysqlpp_datetime.o mysqlpp_dbdriver.o mysqlpp_field_names.o mysqlpp_field_types.o
mysqlpp_manip.o mysqlpp_myset.o mysqlpp_mysql++.o mysqlpp_mystring.o mysqlpp_null.o
mysqlpp_options.o mysqlpp_qparms.o mysqlpp_query.o mysqlpp_result.o mysqlpp_row.o
mysqlpp_scopedconnection.o mysqlpp_sql_buffer.o mysqlpp_sqlstream.o mysqlpp_ssqls2.o
mysqlpp_stadapter.o mysqlpp_tcp_connection.o mysqlpp_transaction.o mysqlpp_type_info.o
mysqlpp_uds_connection.o mysqlpp_utility.o mysqlpp_vallist.o mysqlpp_wnp_connection.o    
 -install_name .../lib/libmysqlpp.3.dylib -lmysqlclient_r   -L…/lib

But when I take a look at libmysqlclient_r, I find that is looks like the _r version is
just a symlink to the single threaded version?

$ ls -l libmysqlclient*
-rwxr-xr-x  1 dkick  staff   4188700 Apr  7 11:42 libmysqlclient.18.dylib*
-rw-r--r--  1 dkick  staff  12209048 Apr  7 11:42 libmysqlclient.a
lrwxr-xr-x  1 dkick  staff        23 Apr  7 11:42 libmysqlclient.dylib@ ->
libmysqlclient.18.dylib
lrwxr-xr-x  1 dkick  staff        20 Apr  7 11:42 libmysqlclient_r.18.dylib@ ->
libmysqlclient.dylib
lrwxr-xr-x  1 dkick  staff        16 Apr  7 11:42 libmysqlclient_r.a@ ->
libmysqlclient.a
lrwxr-xr-x  1 dkick  staff        20 Apr  7 11:42 libmysqlclient_r.dylib@ ->
libmysqlclient.dylib
$

I had built MySQL 5.6.10 from scratch but I can't seem to figure out if this installation
is "normal"; any help?

________________________________

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