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

awesome Notifier #138

Closed txdv closed 12 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, () => {
            var item = queue.Dequeue ();
            if (callback != null) {
                callback (item);
            }
        });
    }

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