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

Re: [Libevent-users] std::threads



On Thu, Mar 17, 2016 at 05:14:22PM +0000, Tomer Heber wrote:
> Hi Azat,
> 
> Correct me if I'm wrong.

Hi Tomer,

Indeed,

> But he is using std c++11 "libraries" which are cross platform.

but if don't talking about cross platform then pthreads are ok on *nix,
and for win32 we have evthread_use_windows_threads()

But if you need cross platform, then you can just write something like
this (untested):
    #include <mutex>

    /** XXX: implement handle recursive locking! */
    static void *cxx_lock_alloc(unsigned /*locktype*/)
    {
        return reinterpret_cast<void *>(new std::mutex);
    }
    static void cxx_lock_free(void *lock_, unsigned /*locktype*/)
    {
        std::mutex *m = reinterpret_cast<std::mutex *>(lock_);
        delete m;
    }
    static int cxx_lock_lock(unsigned /*mode*/, void *lock_)
    {
        std::mutex *m = reinterpret_cast<std::mutex *>(lock_);
        m->lock();
    }
    static int cxx_lock_unlock(unsigned /*mode*/, void *lock_)
    {
        std::mutex *m = reinterpret_cast<std::mutex *>(lock_);
        m->unlock();
    }
    static int use_lock_unlock_profiler(void)
    {
        struct evthread_lock_callbacks cbs = {
            EVTHREAD_LOCK_API_VERSION,
            EVTHREAD_LOCKTYPE_RECURSIVE,
            cxx_lock_alloc,
            cxx_lock_free,
            cxx_lock_lock,
            cxx_lock_unlock,
        };
        evthread_set_lock_callbacks(&cbs);
        // evthread_enable_lock_debugging();

Also you can take a look at
https://github.com/libevent/libevent/blob/master/test/regress_bufferevent.c#L300

> On the other hand, pthread is not cross platform.
> 
> So I think Michael shouldn't use pthreads and instead implement the callbacks (which is actually pretty straightforward).
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.