#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <WinSock2.h>
#include <mysql.h>
#include <windows.h>pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
void *thread_fun(void *);int  main()
{
pthread_t thread_id[10];
int i;
MYSQL mysql;
    if(mysql_init(&mysql)==NULL)
return -1; for( i = 0; i < 10; i++)
{
pthread_create(&thread_id[i], NULL, thread_fun, NULL);
}
for(i = 0; i <  10 ; i++)
{
pthread_join(thread_id[i], NULL);
} return 0;}void *thread_fun(void *ptr)
{
MYSQL mysql;   
MYSQL_RES * result;                        
MYSQL_ROW row;                                const char * host = "127.0.0.1";  
    const char * user = "root";       
    const char * passwd = "123456"; 
    const char * db = "testmysql";                  pthread_mutex_lock(&mutex1);
mysql_thread_init();                        
    if (  mysql_real_connect(&mysql, host, "root", "123456", "testmysql", 0, NULL, 0)  == NULL ) 
    { 
        printf("连接失败,原因是: \n"); 
        fprintf(stderr, " %s\n", mysql_error(&mysql)); 
        exit(1); 
    } 
    else 
    { 
        fprintf(stderr, "连接MySQL成功!!\n"); 
    }     const char * i_query = "select * from children";    if ( mysql_query(&mysql, i_query) != 0 )      
    { 
        fprintf(stderr, "查询失败!\n"); 
        exit(1); 
    } 
    else 
    { 
        if ( (result = mysql_store_result(&mysql)) == NULL )
        { 
            fprintf(stderr, "保存结果集失败!\n"); 
            exit(1); 
        } 
        else 
        { 
            while ( (row = mysql_fetch_row(result)) != NULL ) 
            { 
printf("id is %s\t",row[0]);
                printf("name is %s\t", row[1]);              
                printf("age is %s\t\n", row[2]);              
            }
        } 
 
    } 
pthread_mutex_unlock(&mutex1);
mysql_close(&mysql);
    mysql_free_result(result);
mysql_thread_end();
return 0;
}
这个程序弹出错误是,this handle is already connected。Use 啊separate handle foreach connection。
我想问下,pthread_create(&thread_id[i], NULL, thread_fun, NULL);这个创建线程函数中调用thread_fun函数。这个函数中又建立连接,关闭连接了,那这个程序到底是建立了一个连接,还是10个连接。如果想建立10个connection,该怎样修改

解决方案 »

  1.   

    我的意思是,pthread_create(&thread_id[i], NULL, thread_fun, NULL)中调用thread_fun(),而pthread_fun()中有建立连接,然后关闭连接,那么,mian函数中难道不是建立了十次连接么。初学者,问的问题有可能比较简单,不好意思,
      

  2.   

    本来是要建10个连接的,但是你这几个线程共享了句柄:&mysql
    提示是说,每个线程(连接),都使用各自的MYSQL句柄。这样就不会出错了。
      

  3.   

    谢谢你的回答,我现在知道出错的原因了,但是我还是有点不理解,pthread_create()函数调用了10次thread_fun(),thread_fun()中每次都重新建立连接,断开连接了,为什么还会公用一个?
      

  4.   

    谢谢你的回答,我现在知道出错的原因了,但是我还是有点不理解,pthread_create()函数调用了10次thread_fun(),thread_fun()中每次都重新建立连接,断开连接了,为什么还会公用一个?
    公用一个,是因这你这10个线程中的连接创建都基于同一个MYSQL名柄,共享同一个资源,意味着同享一个数据库连接实例。虽然你试图断开连接,但是你没有使用同步机制,可能有的连接还没有断开,另一个线程就用到了这个句柄。。
      

  5.   

    谢谢你的回答,我现在知道出错的原因了,但是我还是有点不理解,pthread_create()函数调用了10次thread_fun(),thread_fun()中每次都重新建立连接,断开连接了,为什么还会公用一个?
    公用一个,是因这你这10个线程中的连接创建都基于同一个MYSQL名柄,共享同一个资源,意味着同享一个数据库连接实例。虽然你试图断开连接,但是你没有使用同步机制,可能有的连接还没有断开,另一个线程就用到了这个句柄。。
    谢谢你的回答,我知道了,而且你起的好早,起这么早回答帖子,太用功了,向你学习,呵呵。