2015-12-30 86 views
1

我正在开发一个线程化程序,它读取10个线程中的mysql数据库,并将结果作为每个线程的行数打印出来。所以最终的结果是在命令行上有100行计数。问题是当我运行它时,它有时会给出一个错误,说Can't connect to MySQL server on 'localhost' (111) 有时它会返回一个segmentation fault(core dumped)我检查了线程,它们也创建正常,没有错误。无法连接到'localhost'上的MySQL服务器(111)

当我创建只有一个线程它完美地罚款给予预期的输出。 No of Rows of 00 - 6

另一件事是我在红帽服务器运行这个和它运作良好,没有错误,但数为wrong.I创建5个线程和第一线52380是正确的,但对其他人而言,给出了不同的结果结果应该来这样

No of Rows of 00 - 52380 
No of Rows of 01 - 53434 
No of Rows of 02 - 53333 
No of Rows of 03 - 50005 
No of Rows of 04 - 48393 

但实际结果出来是这样的

No of Rows of 00 - 52380 
No of Rows of 00 - 52380 
No of Rows of 01 - 52380 
No of Rows of 01 - 52380 
No of Rows of 01 - 52380 

我编译这个使用

gcc main.c -lpthread `mysql_config --cflags --libs` 

这个问题会导致什么原因。任何人都可以帮助我请这个。这里是我使用的代码。头文件不在这里给出。它包含数据库详细信息。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "main1.h" 
#include <pthread.h> 
#include <mysql/mysql.h> 
#include <mysql/my_global.h> 

int main(int argc, char** argv) { 

    int threadRet[10], i = 0; 
    pthread_t * threadT = (pthread_t *) malloc(10 * sizeof (pthread_t)); 

    for (i = 0; i < 10; i++) { 
     threadRet[i] = pthread_create(&threadT[i], NULL, threadedConnection, &i); 
     printf("thread %d - thread ret %d\n",i,threadRet[i]); 
    } 
    i = 0; 
    for (i = 0; i < 10; i++) { 
     pthread_join(threadT[i], NULL); 
    } 
    return (EXIT_SUCCESS); 
} 



void * threadedConnection(void * parr) { 

    int * j = (int *) parr; 
    int tableNo=*j; 
    long int rowCount = 0; 
    long int totalInserts = 0; 

    MYSQL *con1 = mysql_init(NULL); 
    MYSQL_RES *result; 
    MYSQL_ROW row; 
    con1[tableNo] = mysql_init(NULL); 
    if (mysql_real_connect(con1, dataBase1[0], dataBase1[1], dataBase1[2], dataBase1[3], 0, NULL, 0) == NULL) { 
     fprintf(stderr, "%s\n", mysql_error(con1)); 
     mysql_close(con1); 
     exit(1); 
    } 

    char countQuery[70]; 
    sprintf(countQuery, "SELECT COUNT(*) FROM numberTable where number like '%s%.2d'", "%", *tableNo); 

    if (mysql_query(con1[tableNo], countQuery)) { 
     fprintf(stderr, "%s\n", mysql_error(con1)); 
     mysql_close(con1); 
     exit(1); 
    } 

    result = mysql_store_result(con1);  //Counting 
    row = mysql_fetch_row(result[tableNo]); //line 
    rowCount = strtol((row)[0], NULL, 10); //numbers 
    totalInserts = rowCount;     //numberTable 
    mysql_free_result(result[tableNo]); 

    printf("No of Rows of %.2d - %ld\n", tableNo, totalInserts); 



    mysql_close(con1[tableNo]); 
    } 
+0

问:什么是运行多个线程的目的?问:无关,但为什么您使用弃用的API,而不是[PDO](http://php.net/manual/en/ref.pdo-mysql.php)或[mySqli](http:// php。净/手动/ EN/book.mysqli.php)?看看这里的更多细节:http://php.net/manual/en/mysqlinfo.api.choosing.php – paulsm4

+0

@ paulsm4这是一个应用程序,需要更高的速度。在这个程序中还有另一个没有被开发的进程。计算并插入到另一台mysql服务器。这只是开始部分。 – Laksith

+3

非常重要:1)请不要*针对弃用的“mysql”API编写任何新代码。 2)请不要*期待你的线程中的“printf()'”匹配执行的实际顺序 - 它们不会。 4)检查mySQL配置中的[max_connections](https://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html)。最重要*:请看看你的'mysql_error()'输出,看看你的mySQL错误日志,并且*用一个不错的错误消息*更新你的POST。 – paulsm4

回答

1

我不得不走到一个非常艰难的境地才能最终达到目的。它非常简单,只有一个数组声明才能做到这一点。在我的代码中,在主函数中。创建我使用的线程,当

for (i = 0; i < 10; i++) { 
    threadRet[i] = pthread_create(&threadT[i], NULL, threadedConnection, &i); 
    printf("thread %d - thread ret %d\n",i,threadRet[i]); 
} 

这里是anthor链接,说这个 Pthreads

薄是,当阵列做他们的工作,他们使用过整数i以来的地址该整数被传递并且在for循环中不断增加其值。所以当线程尝试使用它时,它已经增加并且不断增加。所以它会卡住。

因此,要克服它我所做的是我decleared数组作为

for (i = 0; i < 10; i++) { 
    threadRet[i] = pthread_create(&threadT[i], NULL, threadedConnection, &tbl[i]); 
    printf("thread %d - thread ret %d\n",i,threadRet[i]); 
} 

此所做的工作以及在创建线程时

for (i = 0; i < threadCount; i++) { 
    tbl[i] = i; 
} 
i == 0; 

这个阵列elemets获得通过。还有一些更少的修改。我没有在这里提到,因为我认为它们与我的问题无关。他们在主线末端加入线程时,我使用了pthread_attr使它们可以加入其他方式,但它并不完全正确。所以这里是最后的唯一的代码(在这里给出的主要功能,因为变化仅适用于主发生)的

int main(int argc, char** argv) { 

    int threadRet, i = 0; 
    int tbl[threadCount]; 
    void *status; 
    pthread_attr_t attr; 

    for (i = 0; i < threadCount; i++) { 
    tbl[i] = i; 
    } 
    i == 0; 

    pthread_attr_init(&attr); 
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 

    pthread_t * threadT = (pthread_t *) malloc(threadCount * sizeof (pthread_t)); 

    for (i = 0; i < threadCount; i++) { 
    threadRet = pthread_create(&threadT[i], &attr, threadedConnection, &tbl[i]); 
     if (threadRet) { 
      printf("Error creating thread return value of pthread_create() is %d", threadRet); 
     } 
     printf("thread %d - thread ret %d\n", i, threadRet); 
    } 
    i = 0; 

    pthread_attr_destroy(&attr); 

    for (i = 0; i < threadCount; i++) { 
     threadRet = pthread_join(threadT[i], &status); 
     if (threadRet) { 
      printf("Error joining thread return value of pthread_join() is %d", threadRet); 
     } 

    } 

    free(threadT); 
    pthread_exit(NULL); 
    return (EXIT_SUCCESS); 
} 

感谢任何人谁试图帮助环聊贡献他们的时间....

+1

这使我的一天! –

0

if (mysql_real_connect(con1, dataBase1[0], dataBase1[1], dataBase1[2], dataBase1[3], 0, NULL, 0) == NULL) { 
     ^port 

您需要将其默认为3306检查主机名和其他参数的端口。

参见http://dev.mysql.com/doc/refman/5.7/en/mysql-real-connect.html作为参考。

+0

没有。正如我所提到的那样,连接没有问题,因为它对1个线程正常工作。并在服务器上正常工作 – Laksith

+0

请打印所有字符串参数(dataBase)和整数参数的值。这可能会给出正确的图片。 – doptimusprime

相关问题