Mysql in a multithreaded program - my_thread_global_end()

I'm having serious problems with mysql using pthreads. The error I get after ending my program:

"Error in my_thread_global_end(): 1 threads didn't exit"

I called mysql_library_init in main before starting any threads. For the sake of it, I just started 1 thread. After the thread is closed (using pthread_join), I call mysql_library_end in main. In the pthread itself I call mysql_init. For some reason this seems incorrect cause I get the error. I use MySQL 5.6 and link with libmysqlclient.a.

The mysql manual is extremely unclear and contradictory, so I hope someone with a logical mind can explain this to me:

"In a nonmulti-threaded environment, mysql_init invokes mysql_library_init automatically as necessary. However, mysql_library_init is not thread-safe in a multi-threaded environment, and thus neither is mysql_init. Before calling mysql_init, either call mysql_library_init prior to spawning any threads, or use a mutex to protect the mysql_library_init call. This should be done prior to any other client library call."

First line: So mysql_init ONLY invokes mysql_library_init in a NONmulti-threaded environment "when needed" (when is it needed anyway in a NONmulti-threaded environment?) and so I can conlcude from this that mysql_init() thinks it is NOT needed in a multi-threaded environment? I guess not, so fine, I call mysql_library_init in my main... Then I read everywhere I should also call mysql_init within the thread after that. I want each thread to have his own connection, so fine, I also do that so each thread have their own MYSQL struct. But the manual sais mysql_init is not thread safe... Uhm, ok... So just with 1 thread, I still have the problem...

main -> mysql_library_init
main -> create 1 pthread
pthread -> mysql_init
pthread -> mysql_real_connect
pthread -> mysql_close
....

I press Ctrl C after a few seconds (mysql was closed by now in the thread) so the cleaning up starts:

main -> pthread_cancel
main -> pthread_join
main -> mysql_library_end

RESULT: Error in my_thread_global_end: 1 threads didn't exit

........

int main( void )
{
   if ( mysql_library_init( 0, NULL, NULL ) != 0 ) { ... }
   if ( mysql_thread_safe() ) { ... } // This goes fine

   sem_init( &queue.totalStored, 0, 0 );
   pthread_mutex_init( &mutex_bees, NULL );
   pthread_create( &workerbees[tid], &attr, BeeWork, ( void * ) tid );
   pthread_attr_destroy( &attr );

   while ( recv_signal == 0 )
   {
      errno = 0;
      sock_c = accept( sock_s, NULL, NULL );

      if ( ( sock_c == -1 ) && ( errno == EINTR ) )
      {
         // do stuff
         if ( recv_signal == SIGHUP ) { /* do stuff*/ }
      } else { /* do stuff */ }
   }

   // CLEANUP
   close( sock_s );
   RC = pthread_cancel( workerbees[tid] );
   if ( RC != 0 ) { Log( L_ERR, "Unsuccessful pthread_cancel()" ); }

   // WAIT FOR THREADS TO FINISH WORK AND EXIT
   RC = pthread_join( workerbees[tid], &res );
   if ( RC != 0 ) { Log( L_ERR, "Error: Unsuccessful pthread_join()" ); }

   if ( res == PTHREAD_CANCELED )
   { /* print debug stuff */ }
   else { /* print debug stuff */ }

   mysql_library_end();
   sem_destroy( &queue.totalStored );
   exit( 0 );
}

void *BeeWork( void *t )
{
   // DISABLE SIGNALS THAT main() ALREADY LISTENS TO
   sigemptyset( &sigset );
   sigaddset( &sigset, SIGINT );
   sigaddset( &sigset, SIGTERM );
   sigaddset( &sigset, SIGQUIT );
   sigaddset( &sigset, SIGHUP );
   pthread_sigmask( SIG_BLOCK, &sigset, NULL );

   MYSQL *conn;
   conn = mysql_init( NULL );
   if ( ! mysql_real_connect( conn, server, prefs.mysql_user, prefs.mysql_pass, prefs.mysql_db, 0, prefs.mysql_sock, 0 ) ) { /* error */ }
   mysql_close( conn );

   // Do stuff
   ...
   pthread_exit( ( void * ) t );
}

MySQL Bugs: #25621: Error in my_thread_global_end(): 1 threads , Sorry guys, but developing multi-threaded applications without get this error: Error in my_thread_global_end() (Bug #25621) Setting the bug� Glen, this is easy - my_thread_init() is called indirectly from mysql_init(). But I cannot find where my_thread_global_end() is called from - and it is apparently called from somewhere as it is a function that prints the warning.

I guess I can answer my own question, I found out my pthread cleanup handler was not executed (installed with pthread_cleanup_push) and the end of the code with pthread_exit was called sooner than main could cancel the thread. I did a pthread_cleanup_pop( 0 ) and changed it to pthread_cleanup_pop( 1 ) so the cleanup handler also got executed when the thread exits sooner than main could cancel. In this cleanup handler, now mysql_thread_end actually got a chance to run and it fixed the problem.

[#QTBUG-41483] MySql: my_thread_global_end , MySql: my_thread_global_end(): XX threads didn't exit In a multithreaded application when removing a MySql connection the error occurs: QRunnable { public: virtual void run(); }; void Client::run() { { QSqlDatabase db� The test like this: C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqld.exe --defaults-file="c:\program files\mysql\mysql server 5.1\my.ini" --console 071019 9:35:32 InnoDB: Started; log sequence number 0 7040721 071019 9:35:32 [ERROR] Can't open and lock privilege tables: Table 'mysql.serve rs' doesn't exist 071019 9:35:32 [Note] Event

Addition: Apparently, the MySQL client library is not designed to use connection handles (MYSQL*) across threads.

Do not pass a MYSQL* from one thread to another.

Do not use std::async or simlilar in conjunction with MySQL functions. (Code may or may not be executed in a separate thread.)

多线程程序中的Mysql, I'm having serious problems with mysql using pthreads. The error I get after [英] Mysql in a multithreaded program - my_thread_global_end(). 本文翻译自� What I figured out is that the number represents the number of times I opened a mysql connection with driver->connect(). I'm not sure what should I do. The application I am writing is meant to be highly reliable and is meant to be running for days or even months without stopping, so probably I can't ignore this issue.

多線程程序中的Mysql, I'm having serious problems with mysql using pthreads. The error I [英]Mysql in a multithreaded program - my_thread_global_end(). 本文翻译� MySql: my_thread_global_end(): XX threads didn't exit. Log In. Export. XML Word Printable. In a multithreaded application when removing a MySql connection the

7. Using MySQL++ in a Multithreaded Program, Link your program to a thread-aware build of the MySQL C API library. The ConnectionPool can't know how to create() the Connection objects, because that � Ok what you have to do is this, go to your PHP install directory and rename libmysql.dll to libmysql.old. Next head to http://www.php.net/releases/ and grab 5.2.1

MySQL, error in my_thread_global_end � Issue #79 � aio-libs , I use on linux system: unixODBC-2.3.4; mysql-5.6.35; myodbc-5.2.7; python-3.6.0 ; error message, not exception: Error in my_thread_global_end(): 2 threads didn'. to host and review code, manage projects, and build software together. import pyodbc cnxn = pyodbc.connect(dsn) cursor = cnxn.cursor()� The thread has completed a multithreaded repair for a MyISAM table. Repair with keycache. The repair code is using creating keys one by one through the key cache. This is much slower than Repair by sorting. Rolling back. The thread is rolling back a transaction. Saving state

Comments
  • I forgot to mention: the thread is still alive when I press Ctrl C, it was blocking on a semaphore (for other purposes) and will stop blocking as soon as I hit Ctrl C...
  • No I don't, does that command exist? Can't find it in the online manual. If you rather want me to paste some code, I will.