List:MySQL++« Previous MessageNext Message »
From:Adrian Cornish Date:July 11 2014 11:10pm
Subject:Re: Problem with attempt to share a Connection between threads
View as plain text  
Not sure if this will help you, but I use multithreaded mysql++ - here
is a (badly) cut down version of my code - probably doesn't compile
but will give you an idea of how I use it - I run it with approx ~20
threads at the same time


void RunA();
void RunB();
void RunC();

struct MyConnection
{
   MyConnection()
   {
      if(!conn_.set_option(new mysqlpp::ConnectTimeoutOption(600)))
      {
         throw;
      }
      if(!conn_.set_option(new mysqlpp::MultiStatementsOption(true)))
      {
         throw;
      }
      if(!conn_.set_option(new mysqlpp::ReconnectOption(true)))
      {
         throw;
      }
      const Config &config=Config::Instance();
      conn_.connect(config.getDatabase().c_str(),
config.getHost().c_str(), config.getUser().c_str(),
config.getPassword().c_str());
      std::cout << "Connecting: " << conn_.thread_id() << ' ' <<
config.getDatabase() << "@" << config.getHost()
               << " as " << config.getUser() << ":" <<
config.getPassword() << std::endl;
   }
   mysqlpp::Connection &get() { return conn_; }
   mysqlpp::Connection conn_;
};

struct Functor
{
   Functor(void (*func_ptr)())
      :LoadFunction(func_ptr)
   {
   }

   void operator()()
   {
      LoadFunction();
      mysql_thread_end();
   }

   void (*LoadFunction)();
};

int main(int argc, char *argv[])
{
   if(!mysql_thread_safe())
   {
      std::cout << "Not compiled to thread safe mysql library\n";
      exit(1);
   }

   // Setup mysql thread stuff
   if(mysql_library_init(0, 0, 0))
   {
      std::cout << "Failed to init mysql library\n";
      exit(1);
   }
   try
   {
      boost::thread_group threads;

      if(DoA)
      {
         Functor f(RunA);
         threads.create_thread(f);
      }
      if(DoB)
      {
         Functor f(RunB);
         threads.create_thread(f);
      }
      if(DoC)
      {
         Functor f(RunC);
         threads.create_thread(f);
      }
      threads.join_all();
   }
   catch(const std::exception &e)
   {
      std::cout << "EXCEPTION: " << e.what() << std::endl;
   }
   mysql_library_end();
}
void RunA()
{
   MyConnection conn;
   mysqlpp::Query query=conn.get().query();
   try
   {
      query.replacefrom(insertlist.begin(), insertlist.end(), insert_policy);
   }
   catch(const mysqlpp::BadQuery& e)
   {
      std::cout << "EXCEPTION: " << e.what() << std::endl;
   }
}
void RunB()
{
   MyConnection conn;
   mysqlpp::Query query=conn.get().query();
   try
   {
      query.replacefrom(insertlist.begin(), insertlist.end(), insert_policy);
   }
   catch(const mysqlpp::BadQuery& e)
   {
      std::cout << "EXCEPTION: " << e.what() << std::endl;
   }
}
void RunC()
{
   MyConnection conn;
   mysqlpp::Query query=conn.get().query();
   try
   {
      query.replacefrom(insertlist.begin(), insertlist.end(), insert_policy);
   }
   catch(const mysqlpp::BadQuery& e)
   {
      std::cout << "EXCEPTION: " << e.what() << std::endl;
   }
}

On Fri, Jul 11, 2014 at 3:24 PM, Damien Kick <dkick@stripped> wrote:
>
> On Jul 11, 2014, at 4:07 PM, Warren Young <mysqlpp@stripped>
>  wrote:
>
>> Two independent Connection objects shouldn't be sharing any memory, except via
> the MySQL C API.  Therefore, the race is more likely to be be in the C API layer.
>>
>> I've been proven wrong before.  The burden of proof is on you, though.
>
> Ah, well … that does sound like a burden, especially since I haven't been
> trying to prove anything, but rather just try to figure out how to correctly use the API. 
> The workaround I found should let me get a bit further, though, I think.  Thanks for the
> help.  I've learnt a lot about MySQL++.
>
> ________________________________
>
> 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.
>
> --
> MySQL++ Mailing List
> For list archives: http://lists.mysql.com/plusplus
> To unsubscribe:    http://lists.mysql.com/plusplus
>
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