10 messages in com.mysql.lists.win32RE: How thread-safe is mysql_real_con...
FromSent OnAttachments
SGr...@unimin.com07 Oct 2005 09:00 
Mark Papadakis07 Oct 2005 09:33 
SGr...@unimin.com07 Oct 2005 10:02 
John McCaskey07 Oct 2005 10:31 
SGr...@unimin.com07 Oct 2005 11:02 
Lefteris Tsintjelis07 Oct 2005 12:00 
SGr...@unimin.com07 Oct 2005 12:04 
Jeremiah Gowdy11 Oct 2005 00:08 
SGr...@unimin.com11 Oct 2005 06:52 
John McCaskey11 Oct 2005 08:10 
Subject:RE: How thread-safe is mysql_real_connect()?
From:John McCaskey (joh@klir.com)
Date:10/11/2005 08:10:24 AM
List:com.mysql.lists.win32

Wow, thats good to know. Thanks Jeremiah. It is a little strange that the
documentation doesn't mention that this behavior is different under windows and
leads one to believe that calling mysql_thread_init/end is still neccesary.

John

________________________________

From: SGr@unimin.com [mailto:SGr@unimin.com] Sent: Tue 10/11/2005 6:52 AM To: Jeremiah Gowdy Cc: John McCaskey; mys@lists.mysql.com; win@lists.mysql.com Subject: Re: How thread-safe is mysql_real_connect()?

"Jeremiah Gowdy" <jgo@cox.net> wrote on 10/11/2005 03:08:40 AM:

The Windows DLL is thread safe. You do not have to call my_init() and my_thread_init() because Windows DLLs receive events when they are attached to a new process and when they are attached to a new thread in a process. This is one of the nicer features of Windows shared libraries. Other than that, you don't have to do anything special. I am a heavy user of libmysql under Win32. You simply mysql_init() your MYSQL struct, and then mysql_real_connect() and you're ready to mysql_query().

You should not call my_init() or my_thread_init() as the previous poster suggested. This could result in memory leaks.

From libmysql/dll.c

BOOL APIENTRY LibMain(HANDLE hInst,DWORD ul_reason_being_called, LPVOID lpReserved) { switch (ul_reason_being_called) { case DLL_PROCESS_ATTACH: /* case of libentry call in win 3.x */ if (!inited++) { s_hModule=hInst; libmysql_init(); main_thread=GetCurrentThreadId(); } break; case DLL_THREAD_ATTACH: threads++; my_thread_init(); break; case DLL_PROCESS_DETACH: /* case of wep call in win 3.x */ if (!--inited) /* Safety */ { /* my_thread_init() */ /* This may give extra safety */ my_end(0); } break; case DLL_THREAD_DETACH: /* Main thread will free by my_end() */ threads--; if (main_thread != GetCurrentThreadId()) my_thread_end(); break; default: break; } /* switch */ return TRUE; UNREFERENCED_PARAMETER(lpReserved); } /* LibMain */

----- Original Message ----- From: "John McCaskey" <joh@klir.com> To: <SGr@unimin.com>; <win@lists.mysql.com>; <mys@lists.mysql.com> Sent: Friday, October 07, 2005 10:31 AM Subject: RE: How thread-safe is mysql_real_connect()?

Sean,

First let me thank you for all the great posts and info I've seen you put on this list for others.

I've been working in C with MySQL in a very multithreaded environment for several years and think I can explain the thread safety issues clearly. Rather than try to respond point by point to your question I'm going to give a summary and if that doesn't help please respond again and I'll answer specific questions.

First, mysql is in fact pretty much threadsafe when using the _r library. You definitely do need to use the _r library and not the normal one as the SIGPIPE discussion applies to both, the non _r library has additional safety issues surrounding mysql_real_connect() and should not be used. On windows you don't really need to do anything here I believe because "the Windows binaries are by default compiled to be thread-safe." (from http://dev.mysql.com/doc/mysql/en/threaded-clients.html). To validate this in your client code you should in the main() function close to startup use mysql_thread_safe() to verify your linked in version is thread safe.

The next thing you need to do is initialize mysql globally before creating any threads that will use it. Simply call my_init(); in your main thread. After this you can go ahead and create any threads. In the threads you create you need to call mysql_thread_init(); and when you end the thread mysql_thread_end(); in between these calls you can just use mysql as normal and the mysql_real_connect function will be thread safe, you do not need to perform any locking of your own to make only one call at a time or anything along those lines.

Here is some pseudo code of what you need to do:

int main(int argc, char **argv) {

if(!mysql_thread_safe()) { fprintf(stderr, "Not Thread safe!!!"); return 1; }

my_init();

// your regular init code

// create the threads that will use mysql CreateThread(....);

}

void *mysql_thread(void *arg) { mysql_thread_init();

//regular mysql code and whatever else here //use mysql_real_connect and mysql_real_query //and whatever without worrying about thread safety

mysql_thread_end(); }

John A. McCaskey Software Development Engineer Klir Technologies, Inc. joh@klir.com 206.902.2027

-----Original Message----- From: SGr@unimin.com [mailto:SGr@unimin.com] Sent: Friday, October 07, 2005 9:01 AM To: win@lists.mysql.com; mys@lists.mysql.com Subject: How thread-safe is mysql_real_connect()?

<snip>

Thank you very much!!

Shawn Green Database Administrator Unimin Corporation - Spruce Pine