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

[Libevent-users] Disabling callbacks and closing - block until callbacks finished



Situation:
----------

I am building a multithreaded socket server built with Libevent. The event loop is run in its own thread and each callback creates a task into a queue. A pool of worker threads then processes the tasks, (of course ensuring that tasks for any given connection are processed in the correct order).

When a close event arrives, a task is created to close the connection and free all related structures. When this task is processed by a worker thread, it does the following things:

1. Disable all callbacks
2. Close the connection
3. Remove all pending tasks in the task queue for this connection

Problem:
--------

The problem is that after the connection is closed, there could still be events in the Libevent event loop either waiting to be processed in a callback or actually being processed by the event thread (i.e. callback running). This could lead to tasks being created for the connection _after_ step 3 which would result in tasks in the task queue for a closed connection - which I want to avoid.

Solution?
---------

I could use a mutex to lock the connection in the event callbacks, but I do not want to introduce blocking methods in the event loop thread.

I could ensure that a connection is valid when I take a task.

But ideally I would like to the bufferevent close method, or the callback disable methods to block until all curently running callbacks for that bufferevent have finished. This way I could be sure that no tasks can be created after the connection is closed.

Question:
---------

Do the methods work like this, or is there a way to achieve this?

Many thanks in advance.

Nick.


***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.