![](https://csdnimg.cn/release/download_crawler_static/19762263/bg12.jpg)
Running C API Client Programs
Note
If you examine programs in a MySQL source distribution, instead of calls to
pthread_mutex_lock() and pthread_mutex_unlock(), you will see
calls to native_mutex_lock() and native_mutex_unlock(). The latter
functions are defined in the thr_mutex.h header file and map to platform-
specific mutex functions.
• Multiple 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 uses 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().
If a thread does not create the connection to the MySQL database but calls MySQL functions, take the
following into account:
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 any other MySQL functions. It is not thread-safe, so call it
before threads are created, or protect the call with a mutex.
2. Arrange for mysql_thread_init() to be called early in the thread handler before calling any MySQL
function. (If you call mysql_init(), it calls mysql_thread_init() for you.)
3. In the thread, call mysql_thread_end() before calling pthread_exit(). This frees the memory
used by MySQL thread-specific variables.
The preceding notes regarding mysql_init() also apply to mysql_connect(), which calls
mysql_init().
3.5 Running C API Client Programs
If, after an upgrade, you experience problems with compiled client programs, such as Commands out of
sync or unexpected core dumps, the programs were probably compiled using old header or library files. In
this case, check the date of the mysql.h header file and libmysqlclient.a library used for compilation
to verify that they are from the new MySQL distribution. If not, recompile the programs with the new
headers and libraries. Recompilation might also be necessary for programs compiled against the shared
client library if the library major version number has changed (for example, from libmysqlclient.so.17
to libmysqlclient.so.18).
The major shared client library version determines compatibility. (For example, for
libmysqlclient.so.18.1.0, the major version is 18.) Libraries shipped with newer versions of MySQL
are drop-in replacements for older versions that have the same major number. As long as the major library
version is the same, you can upgrade the library and old applications should continue to work with it.
Undefined-reference errors might occur at runtime when you try to execute a MySQL program. If these
errors specify symbols that start with mysql_ or indicate that the libmysqlclient library cannot be
found, it means that your system cannot find the shared libmysqlclient.so library. The solution 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:
• Add the path of the directory where libmysqlclient.so is located to the LD_LIBRARY_PATH or
LD_LIBRARY environment variable.
10