Mysql源码学习——Connection Manager(4)
线程数中,one_thread_scheduler和one_thread_per_connection_scheduler中设置的一个参数。这两者的区别便是是否创建了
一个新的线程来处理到来的连接。one_thread_scheduler是单线程方式,木有新建线程。我们重点研究one_thread_per_connection_scheduler,其设置的add_connection函数为create_thread_to_handle_connection:
create_thread_to_handle_connection(THD *thd)
{
thread_created++;
threads.append(thd); // 创建线程数自增,并加入到threads链表上
pthread_create(&thd->real_id,&connection_attrib,
handle_one_connection,
(void*) thd);//这就是真正创建线程的地方了,函数便是handle_one_connection
}
可见,最后调用了pthread_create函数,这个函数便是创建一个新的线程,新线程的处理函数为handle_one_connection.
5.新线程处理流程
新线程处理函数为handle_one_connection,到此位置,一个新的connection被一个新创建的线程所单独处理。我们看下其中
是如何进行处理的。
handle_one_connection(void *arg)
{
for (;;)
{
lex_start(thd); //初始化词法分析结构体
login_connection(thd); //用户认证,失败报错
prepare_new_connection_state(THD* thd);//Initialize THD to handle queries
while (!net->error && net->vio != 0 && //循环处理command
!(thd->killed == THD::KILL_CONNECTION))
{
if (do_command(thd))
break; //处理失败跳出
}
end_connection(thd); //关闭连接
close_connection(thd, 0, 1);
thread_scheduler.end_thread(thd,1);//结束线程
return 0;
相关新闻>>
- 发表评论
-
- 最新评论 进入详细评论页>>