mysql - 多线程访问MySQL错误

标签 mysql c multithreading pthreads

我写了一个简单的多线程C程序来访问MySQL,除了当 我在每个线程函数中添加 usleep() 或 sleep() 函数。 我在 main 方法中创建了两个 pthread,

int main(){
        mysql_library_init(0,NULL,NULL);
        printf("Hello world!\n");
        init_pool(&p,100);
        pthread_t producer;
        pthread_t consumer_1;
        pthread_t consumer_2;
        pthread_create(&producer,NULL,produce_fun,NULL);
        pthread_create(&consumer_1,NULL,consume_fun,NULL);
        pthread_create(&consumer_2,NULL,consume_fun,NULL);
        mysql_library_end();
}



   void * produce_fun(void *arg){
    pthread_detach(pthread_self());
    //procedure
    while(1){
        usleep(500000);
        printf("producer...\n");
        produce(&p,cnt++);
    }
    pthread_exit(NULL);
}

void * consume_fun(void *arg){
    pthread_detach(pthread_self());
    MYSQL db;
    MYSQL *ptr_db=mysql_init(&db);
    mysql_real_connect();

    //procedure
    while(1){
        usleep(1000000);
        printf("consumer...");
        int item=consume(&p);
        addRecord_d(ptr_db,"test",item);
    }
    mysql_thread_end();
    pthread_exit(NULL);
}

void addRecord_d(MYSQL *ptr_db,const char *t_name,int item){
    char query_buffer[100];
    sprintf(query_buffer,"insert into %s values(0,%d)",t_name,item);
//pthread_mutex_lock(&db_t_lock);
    int ret=mysql_query(ptr_db,query_buffer);
    if(ret){
        fprintf(stderr,"%s%s\n","cannot add record to ",t_name);
        return;
    }

    unsigned long long update_id=mysql_insert_id(ptr_db);
//    pthread_mutex_unlock(&db_t_lock);
    printf("add record (%llu,%d) ok.",update_id,item);
}

程序输出错误如下:

[Thread debugging using libthread_db enabled]
[New Thread 0xb7ae3b70 (LWP 7712)]
Hello world!
[New Thread 0xb72d6b70 (LWP 7713)]
[New Thread 0xb6ad5b70 (LWP 7714)]
[New Thread 0xb62d4b70 (LWP 7715)]
[Thread 0xb7ae3b70 (LWP 7712) exited]
producer...
producer...
consumer...consumer...add record (31441,0) ok.add record (31442,1) ok.producer...
producer...
consumer...consumer...add record (31443,2) ok.add record (31444,3) ok.producer...
producer...
consumer...consumer...add record (31445,4) ok.add record (31446,5) ok.producer...
producer...
consumer...consumer...add record (31447,6) ok.add record (31448,7) ok.producer...
Error in my_thread_global_end(): 2 threads didn't exit
[Thread 0xb72d6b70 (LWP 7713) exited]
[Thread 0xb6ad5b70 (LWP 7714) exited]
[Thread 0xb62d4b70 (LWP 7715) exited]

Program exited normally.

当我在函数addRecord_d中添加pthread_mutex_lock时,错误仍然存​​在。 那么到底问题出在哪里呢?

最佳答案

问题是您太早调用 mysql_library_end() (尤其是线程中的 usleep() )。可以完成主线程并让其他线程继续工作,但不建议这样做。添加 pthread_join() 的解决方案是最好的。您还可以消除 mysql_library_end(),它会起作用。

关于mysql - 多线程访问MySQL错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1914782/

相关文章:

mysql - FROM 后面的第二个词在 MySQL 查询中是什么意思?

mysql - 需要 MySQL 查询帮助,我需要按日期组 by stock_id 获取起始余额和期末余额

mysql - 授予/撤销权限的 SQL 触发器

c++ - 清楚地说明 * 和 & 作为类型的一部分和作为取消引用/address_of 运算符之间的区别

CoreAudio 输入渲染回调从外部音频接口(interface) Mac OS 10.14 Mojave 渲染全 0

c++ - 正确使用 pthread_detach

MYSQL - 查询期间失去与 MySQL 服务器的连接

c++ - 为什么取址运算符获得的指针不是左值?

java - SurfaceView,重新打开应用程序后黑屏

c# - 跳过已在使用的锁定部分