List:Internals« Previous MessageNext Message »
From:Michael Widenius Date:September 21 2001 9:51pm
Subject:Writing a Thread-Safe Client
View as plain text  
Hi!

>>>>> "Matt" == Matt Pauker <mpauker@stripped> writes:

Matt> Hello, I was referred to this list by someone from #mysql -- If I have
Matt> addressed this improperly, I apologize.

No, this list is ok for this.

Matt> I'm in the process of writing a client that communicates to a MySQL server
Matt> through one connection, but does so through a number of pthreads.  I had
Matt> followed all the directions/hints in the MySQL manual regarding 'How To
Matt> Write a Thread-Safe Client', but having just returned from vacation, I
Matt> found a newly updated section with some new information I'm not clear on.

Matt> Previously, I was just calling mysql_init() and mysql_real_connect() on
Matt> startup, and was not doing anything special after spawning new threads
Matt> (which would do queries, making sure to lock).  The new docs, however,
Matt> imply that I will get a core dump doing this, and that I need to call
Matt> mysql_thread_init() upon thread creation, and mysql_thread_end() just
Matt> before the thread is destroyed.  Unfortunately, I can't find any other
Matt> information about these two functions (on the MySQL site or on a google
Matt> search), so I was hoping someone could give me some further
Matt> description.  I'm assuming that mysql_thread_init() is meant to be called
Matt> in my initial thread function (the one passed to pthread_create()), and
Matt> that mysql_thread_end() is meant to be called just before that function
Matt> returns (effectively ending the thread).

The above is right.

Matt> If someone could shed some light on all of this, I would be very
Matt> appreciative -- I am for the most part a MySQL newbie, and I'm just trying
Matt> to get it working as part of a bigger project.  And again, if this is not
Matt> the right forum for this message, I apologize.

From the MySQL manual:

--------------

`mysql_thread_init()'
.....................
 
Description
...........
 
This function needs to be called for each created thread to initialize
thread specific variables.
 
This is automatically called by `my_init()' and `mysql_connect()'.
 
Return Values
.............
 
none.
 
`mysql_thread_end()'
....................
 
Description
...........
 
This function needs to be called before calling `pthread_exit()' to
free memory allocated by `mysql_thread_init()'.
 
Note that this function *is not invoked automatically* by the client
library.  It must be called explicitly to avoid a memory leak.
 
Return Values
.............
 
none.

----------------


To clarify the above a bit:

If you threads calls my_init() or mysql_connect() you don't have to
call mysql_thread_init() (even if it's safe to call it).
If you haven't experienced any problems with your threaded application
before, this is probably what you are doing.

If your applications creates/destroys a lot of threads, you should
call mysql_thead_end() before pthread_exit() to ensure that all
thread-specific memory MySQL used is released.

If you have any suggestions of how to improve the documentation,
please email this to docs@stripped

Regards,
Monty
Thread
Writing a Thread-Safe ClientMatt Pauker21 Sep
  • Writing a Thread-Safe ClientMichael Widenius22 Sep