mysql新到来一个连接后,会为该线程分配一个线程。如果服务器已经有空闲的线程被缓存了,则直接使用。如果没有缓存可用的线程,则
mysql新到来一个连接后,会为该线程分配一个线程。如果服务器已经有空闲的线程被缓存了,则直接使用。如果没有缓存可用的线程,则重新创建一个线程给该连接使用。
/*
scheduler that uses one thread per connection
*/
void create_thread_to_handle_connection(thd *thd)
{
if (cached_thread_count > wake_thread)//判断时候有缓存线程
{
/* get thread from cache */
thread_cache.append(thd);//如果有缓存线程,则将该连接信息交给该线程处理
wake_thread++;
mysql_cond_signal(&cond_thread_cache);//激活被缓存的线程开始处理连接信息
}
else//进入else,表示没有可用的缓存线程了
{
char error_message_buff[mysql_errmsg_size];
/* create new thread to handle connection */
int error;
thread_created++;//创建新线程,线程数+1,
threads.append(thd);
dbug_print(info,((creating thread %lu), thd->thread_id));
thd->prior_thr_create_utime= thd->start_utime= my_micro_time();
if ((error= mysql_thread_create(key_thread_one_connection,
&thd->real_id, &connection_attrib,
handle_one_connection,
(void*) thd)))//创建线程
{
/* purecov: begin inspected */
dbug_print(error,
(can't create thread to handle request (error %d),
error));
thread_count--;//创建失败,则执行-1操作
thd->killed= thd::kill_connection; // safety
mysql_mutex_unlock(&lock_thread_count);
mysql_mutex_lock(&lock_connection_count);
--connection_count;//到此,表示该连接没有被线程处理
mysql_mutex_unlock(&lock_connection_count);
statistic_increment(aborted_connects,&lock_status);
/* can't use my_error() since store_globals has not been called. */
my_snprintf(error_message_buff, sizeof(error_message_buff),//写错误日志
er_thd(thd, er_cant_create_thread), error);
net_send_error(thd, er_cant_create_thread, error_message_buff, null);//向客户端发生错误信息
close_connection(thd);//结束该连接
mysql_mutex_lock(&lock_thread_count);
delete thd;//删除未该连接分配的资源
mysql_mutex_unlock(&lock_thread_count);
return;
/* purecov: end */
}
}
mysql_mutex_unlock(&lock_thread_count);
dbug_print(info,(thread created));//线程创建成功
}
,