• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Implementing a "Synchronous Channel" with dispatch
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Implementing a "Synchronous Channel" with dispatch


  • Subject: Implementing a "Synchronous Channel" with dispatch
  • From: Andreas Grosam <email@hidden>
  • Date: Wed, 14 Sep 2011 14:40:21 +0200

Dear List,

I've implemented a simple "Synchronous Channel" using dispatch lib.
A "synchronous channel" is an actor in which each producer offering an item (via a put operation) must wait for a consumer to take this item (via a get operation), and vice versa.

The following is probably the simplest implementation, which lacks some important features like timeout etc.

But anyway, is the following a proper and *efficient* implementation when using dispatch lib where consumers and producers will be scheduled in queues and the semaphore is implemented using dispatch_semaphore?

Any hints to improve this?


template <typename T>
class SimpleSynchronousChannel {
public:
    SimpleSynchronousChannel()
    : sync_(0), send_(1), recv_(0)
    {
    }

    void put(const T& v) {
        send_.wait();
        value_ = v;
        recv_.signal();
        sync_.wait();
    }

    T get() {
        recv_.wait();
        T result = value_;
        sync_.signal();
        send_.signal();
        return result;
    }

private:
    T           value_;
    semaphore   sync_;
    semaphore   send_;
    semaphore   recv_;
};



benchmark info: if I run concurrently one producer (performing put()) and one consumer (performing get()), I get a throughput of about 200.000 items/sec on a MacBookPro)






class semaphore is a simple wrapper around dispatch_semaphore:


    class semaphore : noncopyable {
    public:
        explicit semaphore(long n) : sem_(dispatch_semaphore_create(n)) {
            assert(sem_);
        }
        ~semaphore() {
            dispatch_release(sem_);
        }
        void signal()  {
            dispatch_semaphore_signal(sem_);
        }
        bool wait()  { return dispatch_semaphore_wait(sem_, DISPATCH_TIME_FOREVER) == 0; }
        bool wait(double timeout_sec)  {
            long result = dispatch_semaphore_wait(sem_,
                                                  timeout_sec >= 0 ?
                                                  dispatch_time(DISPATCH_TIME_NOW, timeout_sec*1e9)
                                                  : DISPATCH_TIME_FOREVER);
            return result == 0;
        }

    private:
        dispatch_semaphore_t sem_;
    };

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Implementing a "Synchronous Channel" with dispatch
      • From: Dave Zarzycki <email@hidden>
  • Prev by Date: Re: NSInvocationOperations mysteriously whacked out of NSOperationQueue
  • Next by Date: Cocoaheads - Silicon Valley September meeting
  • Previous by thread: Re: NSInvocationOperations mysteriously whacked out of NSOperationQueue
  • Next by thread: Re: Implementing a "Synchronous Channel" with dispatch
  • Index(es):
    • Date
    • Thread