List:General Discussion« Previous MessageNext Message »
From:Michael Widenius Date:September 2 1999 9:07am
Subject:How to make a thread-safe client library?
View as plain text  
>>>>> "mhlee" == mhlee  <big5> writes:

mhlee> Hi all,
mhlee> I am not sure if I am posting on the right place...

mhlee> I want to write a threaded server that each thread  queries MySQL with its
mhlee> individual connection. Since I am using C, I read the chapter describes the
mhlee> C API that libmysqlclient provides and how to make a thread-safe client.
mhlee> However, I don't quite understand what does it mean:

mhlee> /********************************/
mhlee> The client is "almost" thread-safe. The biggest problem is that the
mhlee> subroutines in `net.c' that read from sockets are not interrupt-safe. This
mhlee> was done with the thought that you might want to have your own alarm that
mhlee> can break a long read to a server.
mhlee> The standard client libraries are not compiled with the thread options.
mhlee> To get a thread-safe client, use the -lmysys, -lstring and -ldbug libraries
mhlee> and net_serv.o that the server uses.
mhlee> When using a threaded client, you can make great use of the routines in the
mhlee> `thr_alarm.c' file. If you are using routines from the mysys library, the
mhlee> only thing you must remember is to call my_init() first!
mhlee> /*******************************/

mhlee> Does it mean that I have to use -lmysys -lstring -ldbug and net_serv.o to
mhlee> get a socket interrupt safe client?

You should use -lmysqlclient -lmysys -lstring -ldbug and net_serv.o
(The above is not tested;  The important thing is that thr_alarm.o is not in
libmysqlclient)

You also have to thread that handles interrupts.  Check in 
sql/mysqld.cc the calls to:

init_thr_alarm() and end_thr_alarm

You also need a thread that handles interrupts. Something like the
init_signals() and signal_hand() functions.

mhlee> But there will be many undefined symbols in net_serv.o......

net_serv.o shouldn't introduce any undefined symbols.  net_serv.o is
the same as net.c compiled with -DMYSQL_SERVER, which enables the 
interrupt handling.


mhlee> Also:
mhlee> /**************************/
mhlee> All functions except mysql_real_connect() are currently thread-safe. The
mhlee> following notes describe how to compile a thread-safe client library and use
mhlee> it in a thread-safe manner. (The notes below for mysql_real_connect()
mhlee> actually apply to mysql_connect() as well, but since mysql_connect() is
mhlee> deprecated, you should be using mysql_real_connect() anyway.)
mhlee> To make mysql_real_connect() thread-safe, you must recompile the client
mhlee> library with this command:
shell> CPPFLAGS=-DTHREAD_SAFE_CLIENT ./configure ...

mhlee> The resulting `libmysqlclient.a' library is now thread-safe. What this means
mhlee> is that client code is thread-safe as long as two threads don't query the
mhlee> same connection handle returned by mysql_real_connect() at the same time;
mhlee> /**************************/

mhlee> Does it mean that I can forget about the -lmysys -lstring -ldbug and
mhlee> net_serv.o stuff and get a thread-safe ( socket interrupt safe) client
mhlee> library by doing so? So that I can write a threaded server only utilizes
mhlee> libmysqlclient?

You can get a thread safe client by only using libmysqlclient.  The
only thing you don't get by using this is that reads can automaticly
be aborted after a certain timeout. For this you need the thr_alarm
module + a interupt handler.

mhlee> And, is there any way to confirm that I got a thread-safe library? Besides
mhlee> the "CPPFLAGS=-DTHREAD_SAFE_CLIENT ./configure " described above, I also
mhlee> noticed that there is a "--enable-thread-safe-client" option in configure.
mhlee> Do I have to use both of the two options at the same time?

--enable-thread-safe-client only defines the above flag.  This is what 
you should use with configure to keeps things safe for the future.

mhlee> I have tried several times... Can anybody give me a suggestion?
mhlee> Thank you very much.

Regards,
Monty
Thread
How to make a thread-safe client library?李敏宏2 Sep
  • How to make a thread-safe client library?Michael Widenius2 Sep
  • Re: How to make a thread-safe client library?§õ±Ó§»2 Sep
    • Re: How to make a thread-safe client library?Michael Widenius2 Sep
  • Re: How to make a thread-safe client library?sinisa2 Sep
    • Re: How to make a thread-safe client library?Daniel E. White2 Sep
      • Re: How to make a thread-safe client library?sinisa2 Sep