我試圖實現一個服務器,處理許多tcp連接,從100 - 1000個連接/天基於一天的時間。在閱讀了關於每個連接的線程的c10k問題並使用epoll後,我決定使用兩者作爲線程池,並且main將充當調度程序,因此每個新連接都將分配給一個線程。線程工作者和epoll
我有很多問題,我無法在其他地方找到答案。 以下線程安全嗎?在添加新的fd之前,我需要鎖定嗎?
int main()
{
while(i < number_threads)
{
pthread_create(&id , NULL , worker , (void*) epoll_fd[i]);
i++;
}
//is it ok to add the new_sock for the epoll_fd[i] so the thread can pick it up
int y = 0;
while(1) {
new_sock = accept(...);
if (epoll_ctl(epoll_fd[y], EPOLL_CTL_ADD, new_sock, &ev) < 0)
{
print error;
}
y++;
if (y == number_threads)
y = 0;
}
}
void *worker(void *epfd)
{
epoll_wait //start waiting for event
}
每天1000個連接?您是否定位了幻燈片規則? – 2013-03-25 23:04:01
閱讀C標記爲C++,編輯我的評論 – 2013-03-26 02:28:19