jacksonh / manos

Manos is an easy to use, easy to test, high performance web application framework that stays out of your way and makes your life ridiculously simple.
Other
455 stars 61 forks source link

Add INotifier. #139

Closed txdv closed 10 years ago

txdv commented 12 years ago

The Notifier is an awesome tool to write thread safe communication classes. Imagine you have two loops in different threads and you want communicate between them.

For example, something as easy as this could be implemented in order to provide convenient communication between loops in different threads:

class Notifier<T>
{
    Queue<T> queue;
    Notifier notifier;
    Notifier (Context context, Action<T> callback)
    {
        context.CreateNotifier (context, () => {
            T item = null;
            lock (queue) {
                item = queue.Dequeue ();
            }
            if (callback != null) {
                callback (item);
            }
        }).Start();
    }

    public void Notify (T data)
    {
        lock (queue) {
            queue.Enqueue(data);
        }
        notifier.Notify ();
    }
}

Still some locks there, but they are pretty much minimal and are needed if you want to share state between 2 threads.

ghost commented 12 years ago

What about the AsyncWatcher?

txdv commented 12 years ago

I didn't even know what it does... But looks like it does the same.

txdv commented 12 years ago

Could it be that the managed Async Send should have a lock in it? Because multiple other Contexts in different threads could access them at every single point?