[zeromq-dev] context as global/static variable

Nadav Samet thesamet at gmail.com
Tue May 15 21:57:52 CEST 2012


Hi Daniel,

Yes, you're right that your first singleton was not thread safe, and
therefore the constructor might have been invoked more than once causing
multiple calls to WSAStartup() without sufficiently matching calls to
WSACleanup().

As a rule of thumb, avoid static variables that are not POD-types. There's
some background for this advice in Google's C++ style guide at
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Static_and_Global_Variables.
 One of the recommendations the style guide makes is to change your static
global to a pointer type.

You can then allocate the context inside your main() function and only
after that fork the threads.

-Nadav


On Tue, May 15, 2012 at 12:07 PM, Daniel Krikun <krikun.daniel at gmail.com>wrote:

> Just out of curiousity, nobody ever encountered such problem or nobody
> tried to define zmq::context_t as global variable (in c++)?
>
>
> On Wed, May 9, 2012 at 3:23 PM, Daniel Krikun <krikun.daniel at gmail.com>wrote:
>
>> Further, I suspected there maybe a problem due to the singleton not being
>> thread-safe. Thus, I modified the code to something like this:
>>
>> boost::mutex mutex_;
>> zmq::context_t* p_ = 0;
>>
>> zmq::context_t& get_global_context()
>> {
>>        boost::mutex::scoped_lock(mutex_);
>>        if(p_ == 0)
>>             p_ = new zmq::context_t(1);
>>
>>        return *p_;
>> }
>>
>> Well, that worked fine, but apparently, context_t destructor is never
>> invoked (am I correct?). If, for example, I change p_ to some smart
>> pointer, like boost::shared_ptr, which will invoke ~context_t at program
>> shutdown, then the same problem appears.
>> I guess, there is some problem in calling ~context_t at program exit.
>> Ideas?
>>
>>
>> On Wed, May 9, 2012 at 12:39 PM, Daniel Krikun <krikun.daniel at gmail.com>wrote:
>>
>>> Hello,
>>>
>>> I have some c++ code, that uses zeromq. In the code, I have a singleton
>>> wrapping zmq::context_t, like this:
>>>
>>> struct ctx_singleton
>>> {
>>>            zmq::context_t& instance()
>>>            {
>>>                  static zmq::context_t _ctx;
>>>                  return _ctx;
>>>            }
>>> };
>>>
>>> At application shutdown, I get an assertion failure:
>>>   Successful WSASTARTUP not yet perfromed (..\..\..\src\signaler.cpp:124)
>>>
>>> I suspected there was a problem w/ zmq::context_t having global storage,
>>> so I just take some zeromq sample and moved context definition from main
>>> into global scope, and yes, I yields the same failure. I looked at the
>>> stack trace, it seems that some socket/synch. mechanism is already disposed
>>> when zmq_term is invoked and thus the failure.
>>>
>>> I really would like to put zmq::context_t instance, just to make things
>>> easier, and not to pass the instance to all threads. Is there a possibility
>>> to do so?
>>>
>>> I'm using zeromq-2.1.11, visual studio 2008 on Windows XP sp3, 32-bit.
>>>
>>> Thanks,
>>>
>>> --
>>> Daniel Krikun
>>>
>>>
>>
>>
>> --
>> Daniel Krikun
>>
>>
>
>
> --
> Daniel Krikun
>
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120515/e826f1e5/attachment.htm>


More information about the zeromq-dev mailing list