[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] Add multiples bufferevents on one base then lead to some errors.



Hi all,

Sorry to disturb you.Â
I'm writing a server. I plan to use one thread serving ten connections.
I use bufferevent_setcb to register 10 sockets to one base. I also use a global variable to count the number.

I think I can register the bufferevent on one base before I start a pthread to dispatch().
However, I always get the error: [err] event_queue_insert: 0x146dfc8(fd 32) already on queue 2

If I create one thread for one base with only one bufferevent, then the program works well.

Does anyone help me?
Looking forward to your replies.

Thanks,
Fang

Below is my code.Â

on_accept(){
...
 evutil_make_socket_nonblocking(client_fd);

 if (num_dist==0) {
  evbase=event_base_new();
 }
 /* Create a buffer event. */
 bev = malloc(sizeof(struct bufferevent));
 bev = bufferevent_socket_new(evbase, client_fd, BEV_OPT_CLOSE_ON_FREE);

 bufferevent_setcb(bev, buffered_on_read, buffered_on_write, NULL, NULL);

 bufferevent_enable(bev, EV_READ|EV_WRITE|EV_PERSIST);

 num_dist++;
 if (num_dist==10) {
 tid=malloc(sizeof(pthread_t));
 pthread_mutex_lock(&mutex);
 total_thread++;
 printf("%d thread creates.\n",total_thread);
 pthread_mutex_unlock(&mutex);
 pthread_create(&tid, NULL, &server_job_function, (void *) evbase);
 num_dist=0;
 }

}