[zeromq-dev] Race conditions when creating sockets from different threads?

Steven McCoy steven.mccoy at miru.hk
Mon Feb 8 11:10:43 CET 2010


On 8 February 2010 17:41, Pavel Gushcha <pavimus at gmail.com> wrote:

> Sometimes my multithreaded application is crashed while creating
> sockets. I wrote simple test (attached) that illustrates the problem.
>
> Backtrace is following:
>
> #0  0x0000003b2fa81074 in ?? () from /lib/libc.so.6
> #1  0x0000003b2fa7f47a in memmove () from /lib/libc.so.6
> #2  0x00007f7b15b3d063 in std::vector<zmq2::socket_base_t*,
> std::allocator<zmq2::socket_base_t*>
> >::_M_insert_aux(__gnu_cxx::__normal_iterator<zmq2::socket_base_t**,
> std::vector<zmq2::socket_base_t*, std::allocator<zmq2::socket_base_t*>
> > >, zmq2::socket_base_t* const&) () from /usr/lib/libzmq.so.0
>

Adding the new socket is an STL vector underneath and is not thread-safe,
which is a bit strange considering every other part of create_socket is
locked,

    threads_sync.lock ();
    app_thread_t *thread = choose_app_thread ();
    if (!thread) {
        threads_sync.unlock ();
        return NULL;
    }
    threads_sync.unlock ();


    socket_base_t *s = thread->create_socket (type_);
    if (!s)
        return NULL;

    term_sync.lock ();
    sockets++;
    term_sync.unlock ();


    return s;



Underneath create_socket:

    sockets.push_back (s);

-- 
Steve-o
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100208/044c7d9a/attachment.htm>


More information about the zeromq-dev mailing list