- 论坛徽章:
- 0
|
本帖最后由 liujian0616 于 2014-09-05 14:39 编辑
之前有个关于多线程的帖子我看了,时间过去太久,没什么参考价值,而且我在测试代码里也做到了 每个线程使用自己的tracker server ,还是报错
目前我需要在项目中用到fastdfs,但是项目总体架构用的是多线程模型,我在测试的时候发现client的api在多线程下是不安全的,会报段错误,加锁运行就没问题,但是加锁后这效率就太低了,不能支持大并发了
下面是我的测试代码,是根据fish您的代码改的:- /**
- * Copyright (C) 2008 Happy Fish / YuQing
- *
- * FastDFS may be copied only under the terms of the GNU General
- * Public License V3, which may be found in the FastDFS source kit.
- * Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
- **/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include "fdfs_client.h"
- #include "logger.h"
- #include <getopt.h>
- #include <pthread.h>
- typedef struct test_fileinfo_s test_fileinfo_t;
- struct test_fileinfo_s{
- char *filename;
- char storage_ip[16];
- char fileid[128];
- int threadid;
- long long time_used;
- };
- static test_fileinfo_t files[] = {
- {"/usr/local/include/fastcommon/common_define.h", "", "", 0, 0},
- {"/usr/local/include/fastcommon/connection_pool.h", "", "", 0, 0},
- {"/usr/local/include/fastcommon/fast_task_queue.h", "", "", 0, 0},
- {"/usr/local/include/fastcommon/base64.h", "", "", 0, 0},
- {"/usr/local/include/fastcommon/hash.h", "", "", 0, 0}
- };
- pthread_mutex_t g_mutex;
- void usage(){
- printf("-t thread num \n"
- "-h print this message");
- return;
- }
- void* routine(void *param)
- {
- //pthread_mutex_lock(&g_mutex); //不加锁会有段错误
- test_fileinfo_t *file = (test_fileinfo_t *)param;
- char *conf_filename = "/etc/fdfs/client.conf";
- char *local_filename = NULL;
-
- char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
- ConnectionInfo *pTrackerServer;
- int result;
- int store_path_index;
- ConnectionInfo storageServer;
- char file_id[128];
-
- log_init();
- g_log_context.log_level = LOG_ERR;
- ignore_signal_pipe();
- if ((result=fdfs_client_init(conf_filename)) != 0)
- {
- return result;
- }
- pTrackerServer = tracker_get_connection();
- if (pTrackerServer == NULL)
- {
- fdfs_client_destroy();
- return errno != 0 ? errno : ECONNREFUSED;
- }
- local_filename = file->filename;
- *group_name = '\0';
-
- if ((result=tracker_query_storage_store(pTrackerServer, \
- &storageServer, group_name, &store_path_index)) != 0)
- {
- fdfs_client_destroy();
- fprintf(stderr, "tracker_query_storage fail, " \
- "error no: %d, error info: %s\n", \
- result, STRERROR(result));
- return result;
- }
- result = storage_upload_by_filename1(pTrackerServer, \
- &storageServer, store_path_index, \
- local_filename, NULL, \
- NULL, 0, group_name, file_id);
- if (result == 0)
- {
- printf("%s\n", file_id);
- }
- else
- {
- fprintf(stderr, "upload file fail, " \
- "error no: %d, error info: %s\n", \
- result, STRERROR(result));
- }
- tracker_disconnect_server_ex(pTrackerServer, true);
- fdfs_client_destroy();
- //pthread_mutex_unlock(&g_mutex); //不加锁会有段错误
- return result;
- }
- int create_thread(test_fileinfo_t *file, void*(*routine)(void*param)){
- int result = 0;
- pthread_t tid;
- printf("create a thread\n");
- pthread_create(&tid, NULL, routine, file);
-
- return result;
- }
- int main(int argc, char** argv){
- if (argc < 2){
- usage();
- }
- char ch;
- int thread_num = 0;
- int i = 0;
- while ((ch=getopt(argc, argv, "t:h")) != EOF) {
- switch (ch){
- case 't':
- thread_num = atoi(optarg);
- break;
- case 'h':
- default:
- usage();
- break;
- }
- }
- pthread_mutex_init(&g_mutex, NULL);
- for (i=0; i<thread_num; i++){
- files[i].threadid = i;
- create_thread(files+i, routine);
- }
- while (1){
- sleep(2);
- }
- }
复制代码 附上core文件调试的堆栈信息:- (gdb) where
- #0 0x00007f141e45279a in _int_free () from /lib64/libc.so.6
- #1 0x00007f141e996e29 in iniFreeContext (pContext=0x7f141d7d3c30) at ../common/ini_file_reader.c:506
- #2 0x00007f141ebb615c in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:408
- #3 0x0000000000401251 in routine (param=0x6023e8) at test3.c:66
- #4 0x00007f141e7759d1 in start_thread (arg=0x7f141d7d4700) at pthread_create.c:301
- #5 0x00007f141e4c2b5d in clone () from /lib64/libc.so.6
- (gdb) thread apply all bt
- Thread 6 (Thread 0x7f141cdd3700 (LWP 1674)):
- #0 0x00007f141e453ded in _int_malloc () from /lib64/libc.so.6
- #1 0x00007f141e4549a1 in malloc () from /lib64/libc.so.6
- #2 0x00007f141e997446 in iniDoLoadItemsFromBuffer (content=<value optimized out>, pContext=0x7f141cdd2c30) at ../common/ini_file_reader.c:446
- #3 0x00007f141e996fce in iniDoLoadFromFile (szFilename=0x7f141cdd2b00 "/etc/fdfs/client.conf", pContext=0x7f141cdd2c30) at ../common/ini_file_reader.c:214
- #4 0x00007f141e997888 in iniLoadFromFile (szFilename=0x401ae8 "/etc/fdfs/client.conf", pContext=0x7f141cdd2c30) at ../common/ini_file_reader.c:161
- #5 0x00007f141ebb6121 in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:398
- #6 0x0000000000401251 in routine (param=0x602490) at test3.c:66
- #7 0x00007f141e7759d1 in start_thread (arg=0x7f141cdd3700) at pthread_create.c:301
- #8 0x00007f141e4c2b5d in clone () from /lib64/libc.so.6
- Thread 5 (Thread 0x7f1417fff700 (LWP 1675)):
- #0 0x00007f141e453ded in _int_malloc () from /lib64/libc.so.6
- #1 0x00007f141e4549a1 in malloc () from /lib64/libc.so.6
- #2 0x00007f141e997446 in iniDoLoadItemsFromBuffer (content=<value optimized out>, pContext=0x7f1417ffec30) at ../common/ini_file_reader.c:446
- #3 0x00007f141e996fce in iniDoLoadFromFile (szFilename=0x7f1417ffeb00 "/etc/fdfs/client.conf", pContext=0x7f1417ffec30) at ../common/ini_file_reader.c:214
- #4 0x00007f141e997888 in iniLoadFromFile (szFilename=0x401ae8 "/etc/fdfs/client.conf", pContext=0x7f1417ffec30) at ../common/ini_file_reader.c:161
- #5 0x00007f141ebb6121 in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:398
- #6 0x0000000000401251 in routine (param=0x602538) at test3.c:66
- #7 0x00007f141e7759d1 in start_thread (arg=0x7f1417fff700) at pthread_create.c:301
- #8 0x00007f141e4c2b5d in clone () from /lib64/libc.so.6
- Thread 4 (Thread 0x7f13fffff700 (LWP 1676)):
- #0 0x00007f141e996d0f in iniGetBoolValue (szSectionName=<value optimized out>, szItemName=<value optimized out>, pContext=<value optimized out>,
- bDefaultValue=false) at ../common/ini_file_reader.c:624
- #1 0x00007f141ebb084f in fdfs_connection_pool_init (config_filename=0x401ae8 "/etc/fdfs/client.conf", pItemContext=0x7f13ffffec30)
- at ../tracker/fdfs_shared_func.c:842
- #2 0x00007f141ebb6004 in fdfs_client_do_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf", iniContext=0x7f13ffffec30)
- at client_func.c:319
- #3 0x00007f141ebb6152 in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:406
- #4 0x0000000000401251 in routine (param=0x6025e0) at test3.c:66
- #5 0x00007f141e7759d1 in start_thread (arg=0x7f13fffff700) at pthread_create.c:301
- #6 0x00007f141e4c2b5d in clone () from /lib64/libc.so.6
- Thread 3 (Thread 0x7f141e1d5700 (LWP 1672)):
- #0 0x00007f141e996d0f in iniGetBoolValue (szSectionName=<value optimized out>, szItemName=<value optimized out>, pContext=<value optimized out>,
- ---Type <return> to continue, or q <return> to quit---
- bDefaultValue=false) at ../common/ini_file_reader.c:624
- #1 0x00007f141ebb084f in fdfs_connection_pool_init (config_filename=0x401ae8 "/etc/fdfs/client.conf", pItemContext=0x7f141e1d4c30)
- at ../tracker/fdfs_shared_func.c:842
- #2 0x00007f141ebb6004 in fdfs_client_do_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf", iniContext=0x7f141e1d4c30)
- at client_func.c:319
- #3 0x00007f141ebb6152 in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:406
- #4 0x0000000000401251 in routine (param=0x602340) at test3.c:66
- #5 0x00007f141e7759d1 in start_thread (arg=0x7f141e1d5700) at pthread_create.c:301
- #6 0x00007f141e4c2b5d in clone () from /lib64/libc.so.6
- Thread 2 (Thread 0x7f141efd5700 (LWP 1671)):
- #0 0x00007f141e486ced in nanosleep () from /lib64/libc.so.6
- #1 0x00007f141e486b60 in sleep () from /lib64/libc.so.6
- #2 0x0000000000401491 in main (argc=<value optimized out>, argv=<value optimized out>) at test3.c:150
- Thread 1 (Thread 0x7f141d7d4700 (LWP 1673)):
- #0 0x00007f141e45279a in _int_free () from /lib64/libc.so.6
- #1 0x00007f141e996e29 in iniFreeContext (pContext=0x7f141d7d3c30) at ../common/ini_file_reader.c:506
- #2 0x00007f141ebb615c in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:408
- #3 0x0000000000401251 in routine (param=0x6023e8) at test3.c:66
- #4 0x00007f141e7759d1 in start_thread (arg=0x7f141d7d4700) at pthread_create.c:301
- #5 0x00007f141e4c2b5d in clone () from /lib64/libc.so.6
复制代码 |
|