我在我的应用程序中使用了两个 mysql 对象。在退出线程之前,我只需要调用mysql_thread_end一次。是否有任何函数/回调以便它在退出线程之前调用?
附上示例程序
using namespace std;
class MySql
{
public:
MySql()
{
mysql = mysql_init(NULL);
}
~MySql()
{
if(mysql)
{
mysql_close(mysql);
}
// Wrong: this will clear thread specific values twice. it may lead to crash the application.
// mysql_thread_end();
}
int Connect(const char *host,
const char *user,
const char *passwd,
const char *db,
unsigned int port,
const char *unix_socket,
unsigned long clientflag)
{
if(!mysql_real_connect(mysql, host,user,
passwd, db, port, unix_socket, clientflag))
{
return 1;
}
return 0;
}
int Execute(const char *query)
{
int status = mysql_query(mysql, query);
return status;
}
private:
MYSQL * mysql;
};
void createthreads();
static void StartThread(void * Arg);
int main(int argc, _TCHAR* argv[])
{
int i = mysql_library_init(0, NULL, NULL);
createthreads();
mysql_library_end();
return 0;
}
void createthreads()
{
PRThread *thread;
thread = PR_CreateThread(PR_USER_THREAD, StartThread, (void *)NULL, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
//Sleep(2000);
PR_JoinThread(thread);
}
void StartThread(void * Arg)
{
MySql mysqlsrc;
MySql mysqldest;
//Production server
if(!mysqlsrc.Connect("localhost","root", "root", NULL, 3306, NULL, 0))
{
cout<<"Connection to Src Failed";
return;
}
//Test Server
if(!mysqldest.Connect("localhost","root", "root", NULL, 3306, NULL, 0))
{
cout<<"Connection to Destn Failed";
return;
}
/// Sync databases.
}