从hiredis使用出core谈谈redis多线程的使用

 在实际工作中,我需要使用redis的客户端去连接redis,于是选择了hiredis客户端(公司强推)。  hiRedis 是 Redis 官方指定的 C 语言客户端开发包,支持 Redis 完整的命令集、管线以及事件驱动编程。

1、情景描述

1.1 使用场景

 

      一个epool模型的服务器不断接受外界请求,这个服务器框架给用户预留一个回调函数(多线程),回调函数为用户自己去实现的业务逻辑,其中redis的使用就需要在这个回调函数内部实现。

1.2 初步实现方案

      在程序启动的时候,我就初始化redis的连接,获得hiredis句柄。然后把hiredis句柄传入到线程函数里面。让其做相应的业务逻辑。

1.3 结果

      很不幸,一次请求都没问题,做压力测试,同时开20个线程访问,程序立即出core。

      线上出core如下:

01 (gdb) bt
02 #0  0x000000302af2e2ed in raise () from /lib64/tls/libc.so.6
03 #1  0x000000302af2fa3e in abort () from /lib64/tls/libc.so.6
04 #2  0x000000302af62db1 in __libc_message () from /lib64/tls/libc.so.6
05 #3  0x000000302af6888e in _int_free () from /lib64/tls/libc.so.6
06 #4  0x000000302af6a12d in _int_realloc () from /lib64/tls/libc.so.6
07 #5  0x000000302af6b39c in realloc () from /lib64/tls/libc.so.6
08 #6  0x0000000000dc2269 in sdscatlen (s=Variable "s" is not available.
09 ) at sds.c:97
10 #7  0x0000000000dc1d40 in __redisAppendCommand (c=0x16fa1d0, cmd=Variable "cmd" is not available.
11 ) at hiredis.c:1186
12 #8  0x0000000000dc1d97 in redisvAppendCommand (c=0x16fa1d0, format=Variable "format" is not available.
13 ) at hiredis.c:1206
14 #9  0x0000000000dc1eed in redisvCommand (c=0x16fa1d0, format=Variable "format" is not available.
15 ) at hiredis.c:1267
16 #10 0x0000000000dc1fb6 in redisCommand (c=Variable "c" is not available.
17 ) at hiredis.c:1276
18 #11 0x0000002b1a8e6310 in Default_Handler::get_batch_redis (this=0x1ff41f0, redis_ins=0x175a7d0, dataid=6202, buf_num=12, res_num=6, key_sign=0x2bd67cb3c8,
19     res_lens=0x2bd5f54208, res_buf=0x2bd5f54398 "") at default_handler.cpp:659
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net

猜你喜欢

转载自www.cnblogs.com/sjwudhwhhw/p/10442917.html