Oracle 5.0 Reference Manual page 2154

Table of Contents

Advertisement

The client library is almost thread-safe. The biggest problem is that the subroutines in
from sockets are not interrupt-safe. This was done with the thought that you might want to have your
own alarm that can break a long read to a server. If you install interrupt handlers for the
interrupt, socket handling should be thread-safe.
To avoid aborting the program when a connection terminates, MySQL blocks
to mysql_library_init(), mysql_init(), or mysql_connect(). To use your own
handler, first call mysql_library_init(), then install your handler.
Current binary distributions should have both a normal client library, libmysqlclient, and a thread-
safe library, libmysqlclient_r. For threaded clients, link against the latter library. If "undefined
symbol" errors occur, in most cases this is because you have not included the thread libraries on the
link/compile command.
The thread-safe client library, libmysqlclient_r, is thread-safe per connection. You can let two
threads share the same connection with the following caveats:
• Multiple threads cannot send a query to the MySQL server at the same time on the same
connection. In particular, you must ensure that between calls to
mysql_store_result()
have a mutex lock around your pair of
mysql_store_result()
connection.
If you use POSIX threads, you can use
pthread_mutex_unlock()
• Many threads can access different result sets that are retrieved with mysql_store_result().
• To use mysql_use_result(), you must ensure that no other thread is using the same connection
until the result set is closed. However, it really is best for threaded clients that share the same
connection to use mysql_store_result().
You need to know the following if you have a thread that did not create the connection to the MySQL
database but is calling MySQL functions:
When you call mysql_init(), MySQL creates a thread-specific variable for the thread that is used
by the debug library (among other things). If you call a MySQL function before the thread has called
mysql_init(), the thread does not have the necessary thread-specific variables in place and you are
likely to end up with a core dump sooner or later. To avoid problems, you must do the following:
1. Call
mysql_library_init()
before threads are created, or protect the call with a mutex.
2. Arrange for
MySQL function. If you call mysql_init(), it will call
3. In the thread, call
memory used by MySQL thread-specific variables.
The preceding notes regarding
mysql_init().
20.6.3.3. Running C API Clients
Undefined-reference errors might occur at runtime when you try to execute a MySQL program. If these
errors specify symbols that start with
found, it means that your system cannot find the shared
to this problem is to tell your system to search for shared libraries in the directory where that library is
located. Use whichever of the following methods is appropriate for your system:
Building and Running C API Client Programs
in one thread, no other thread uses the same connection. You must
returns, the lock can be released and other threads may query the same
to establish and release a mutex lock.
before any other MySQL functions. It is not thread-safe, so call it
mysql_thread_init()
mysql_thread_end()
mysql_init()
mysql_
and
mysql_query()
pthread_mutex_lock()
to be called early in the thread handler before calling any
mysql_thread_init()
before calling pthread_exit(). This frees the
also apply to mysql_connect(), which calls
or indicate that the
libmysqlclient.so
2134
SIGPIPE
and
mysql_query()
mysql_store_result()
and
for you.
library cannot be
libmysqlclient
library. The solution
that read
net.c
SIGPIPE
on the first call
SIGPIPE
calls. After

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents