Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1858431
  • 博文数量: 211
  • 博客积分: 464
  • 博客等级: 下士
  • 技术积分: 3794
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-24 18:25
个人简介

阿弥陀佛

文章分类

全部博文(211)

文章存档

2020年(2)

2019年(3)

2018年(5)

2017年(6)

2016年(10)

2015年(9)

2014年(73)

2013年(90)

2012年(13)

分类: 架构设计与优化

2013-11-14 16:48:21

memcached的通信机制是有一个主线程负责监听来的请求,然后main thread负责将得到的请求分发到各个worker thread中。
每个worker thread维护一个请求队列。
主线程通过如下方式与worker 线程交互:
与线程相关的核心代码都在thread.c当中。

点击(此处)折叠或打开

  1. /*
  2.  * Dispatches a new connection to another thread. This is only ever called
  3.  * from the main thread, either during initialization (for UDP) or because
  4.  * of an incoming connection.
  5.  */
  6. void dispatch_conn_new(int sfd, enum conn_states init_state, int event_flags,
  7.                        int read_buffer_size, enum network_transport transport) {
  8.     CQ_ITEM *item = cqi_new();
  9.     char buf[1];
  10.     int tid = (last_thread + 1) % settings.num_threads;

  11.     LIBEVENT_THREAD *thread = threads + tid;

  12.     last_thread = tid;

  13.     item->sfd = sfd;
  14.     item->init_state = init_state;
  15.     item->event_flags = event_flags;
  16.     item->read_buffer_size = read_buffer_size;
  17.     item->transport = transport;

  18.     cq_push(thread->new_conn_queue, item);

  19.     MEMCACHED_CONN_DISPATCH(sfd, thread->thread_id);
  20.     buf[0] = 'c';
  21.     if (write(thread->notify_send_fd, buf, 1) != 1) {
  22.         perror("Writing to thread notify pipe");
  23.     }
  24. }
主线程通过轮询将请求发送到相应的线程中。threads是一个全局的变量,cq_push将写请求放入到该线程的队列中。向thread的管道中写入一个字节的内容,通知工作线程,处理自己的线程队列里面的任务。
阅读(3178) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~