求指点:关于LINUX C MYSQL多线程编程问题
本人遇到一个蛋疼的问题,要开启10000个线程来向数据库插入数据。以下是代码:
#include <stdio.h>#include <pthread.h>#include <string.h>#include <stdlib.h>#include <errno.h>#include <syslog.h>#include <unistd.h>#include <time.h>#include <math.h>#include "/usr/local/mysql/include/mysql.h"#define NTHREADS 300#define PREMAX 300pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;int counter = 0;void *thread_fun(void *);const char *db_host = "localhost"; const char *db_user = "root";const char *db_pass = "linkpower";const char *db_name = "lprcms_wn";void log_error(char *msg){ FILE *fd; fd = fopen("file.txt", "a+"); if (fd){ fputs(msg, fd); fclose(fd); }}void main(){ pthread_t thread_id[NTHREADS]; int i; int j; int k; int times; int start = time(NULL); mysql_library_init(0,NULL,NULL); times = ceil(NTHREADS / PREMAX); for(i = 0; i < times; i++){ for( k = PREMAX * i; k < PREMAX * (i + 1); k++){ pthread_create(&thread_id[k], NULL, thread_fun, &k); } for(k = PREMAX * i; k < PREMAX * (i + 1); k++){ pthread_join(thread_id[k], NULL); } usleep(400000); int middle = time(NULL); int mid_diff = middle - start; printf("Middle Diff:%d\n", mid_diff); } mysql_library_end(); int end = time(NULL); int diff = end - start; printf("Diff:%d\n", diff);}void *thread_fun(void *ptr){ int *tag; tag = (int *)ptr; //printf("Thread NO%d. %ld \n", *tag, pthread_self()); int c = 0; int flag = 0; MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; pthread_mutex_lock(&mutex1); if(conn = mysql_init(NULL)){ if(NULL != mysql_real_connect(conn, db_host, db_user, db_pass, db_name, 0, NULL, 0)){ if(0 == mysql_query(conn, "INSERT INTO demo1(indexVal) VALUES ('1')")){ res = mysql_store_result(conn); mysql_free_result(res); } else { log_error((char *)tag); //usleep(1000); } } else{ char *msg1 = "Connect error! \n"; printf("Pthread NO. %d \n", counter); //usleep(1000); log_error(msg1); msg1 = NULL; } } else { char *e_msg = "INITIAL ERROR! \n"; log_error(e_msg); e_msg = NULL; } pthread_mutex_unlock(&mutex1); mysql_close(conn); mysql_thread_end(); pthread_exit( (void *)0 );}