How to use boost::log named_scope with boost::asio::yield_context? - c++

The thing is that when there are coroutines they can work in random order, and they can end up but
BOOST_LOG_NAMED_SCOPE(...)
keeps scope's name on each stackframe being oblivious to the fact that those stackframes are not nested, so they can be destructed in any order, not to mention that it's not that hard to put on a scenario where the scope according to boost::log::named_scope is different from the actual one.
How to make boost:log compatible with boost::coroutine and boost::context ?

Named scopes in Boost.Log are maintained per-thread, using a thread-local storage, and consequently are incompatible with coroutines.
If you want a similar functionality with coroutines, you will have to implement a separate attribute that maintains a list of scopes in a coroutine-local storage. You can use Boost.Intrusive to implement the list itself. On Windows, you could use fiber-local storage to place the list. On other systems you will probably have to use the stack.
The above applies to stackful coroutines. Stack-less coroutines do not have a dedicated stack, so it is not possible to maintain a dedicated list of scopes that pertains to activity of a given coroutine.

Related

Why is there no clear() function for the inbuilt stack interface in C++?

I have to empty a stack before using it further more. I do understand that it can be done like:
while (!mystack.empty()) { mystack.pop(); }
Is there a specific reason for not having this function? or its just that the first time it was made, no one felt its requirement and has been just left out
Also, the stack interface in Java does have a clear() function.
While it would possibly be more readable to have an explicit .clear(), even without it you can empty a stack like this:
mystack = {};
As molbdnilo mentioned within the comments, you have to distinguish between standard containers and container adapters. std::stack is a container adapter, not a container. There are several reasons, why these adapters have to reduce their assumptions about characteristics of used inner container types as far as possible. A relevant one is time complexity (theoretical, accidental) for instance, that might differ a lot between possible underlying containers here. A further relevant aspect can be the requirement to be consistent to several access schemes within parallel working environments (parallel reads and writes), although that might not be relevant here specifically to the clear-functionality.
And in general, it follows a simple software design rule: Do not inflate top-level interfaces with too many assumptions about possible inner implementations and possible usage-scenarios that might occur for your data type but are not directly related to its core-characteristics. Directly clearing an 'abstract' stack can introduce a lot of confusion in doubt and error-prone misusage of objects of this type since a stack often represents more than just a simple partial ordering, it commonly represents a history. Semantically, a direct clear-operation can be seen as a design attack for several stack-related scenarios here: "Forget what I've done and thought so far with and about the stack, let's try something totally different..." Re-assigning is the superior approach therefore here in terms of proprtionality between the issues mentioned here since you explicitly introduce a totally new object (while the previous one might still live within a shared_ptr for instance somewhere else, unaffected by the clearing if required).
Run a while loop until the stack is empty to pop all the elements
while(!stack.empty()){
stack.pop();
}

Does set_new_handler store new_handler in thread local storage?

It has been asked whether set_new_handler is safe in a multi-threaded environment. I would like to know if C++11 and later implementations of the C++ standard library use the thread_local feature to store the std::new_handler. If implementations do not use thread_local, why not? That would seem to make the feature a bit more robust in multi-threaded programs.
I also don't really see how set_new_handler would work for a class that overloads new and sets a std::new_handler and creates (and owns reference to) an object that also overloads new and sets its own std::new_handler. I would expect that would be especially egregious if the owning class has options to reap/free memory, while the owned object decides to call abort/terminate.
I expect the usual advice is to use nothrowversion of new and forget set_new_handler. If that is a universal opinion, why isn't set_new_handler a deprecated feature in the standard library?
The new_handler is not, and was never expected to be, a thread-local construct. It was always global and that is by design.
The new handler is intended to be set once and left alone; it's not meant for temporary changes that are local to a single execution thread. You can build such a facility for yourself, by creating a new handler that defers its operations to a thread_local variable which your code can set. But the C++ new handler feature itself is not meant for such use cases.
Now, one could say that this was a legacy decision. C++98/03's memory model does not consider the possibility of threading. So any pre-C++11 threaded implementations essentially decided for themselves which operations were thread-safe, were thread-local, and so forth. Implementations that decided to implement the new-handler as a global construct were no less correct than implementations that made it per-thread. Indeed, implementations probably settled on the global option at some point, and C++11 just adopted it. And changing it to be thread-local would break peoples' code.
But at the same time, the point of the new handler is to allow the user to change the default handling of allocation failures. Default error handing is not a thread-local concept; it's something that is universal throughout the program. Much like the termination function and so forth, there is precisely one that is expected to be used.
To put it simply, code that used a class-local new handler was always wrong to some degree, even in the C++98/03 days. It's just that until C++11 standardized threading, it was something that you could get away with (at least as far as the standard was concerned). And it isn't deprecated because it is useful for its intended purpose. nothrow is only useful locally; set_new_handler is useful globally.

Avoid local() calls for tbb enumerable_thread_specific variables

I have a code which uses tbb::enumerable_thread_specific variables, and in the deep place of the call stack the thread local variables are used. The naive implementation leads to a lot of local() function calls.
Now I want to avoid local() function calls by passing parameters hierarchically. Is there a simpler way of doing this? I have many places with local() function calls if I do not pass Foo as a parameter, but the code would be messy if I do. I have been looking for possible usage of an array with size equal to the number of threads, and use thread-id to access the thread local variable, but it seems tbb does not provide that (in contrast to omp_get_thread_num() in OpenMP).
See more descriptions here:
https://software.intel.com/en-us/forums/intel-threading-building-blocks/topic/804043
Repeating and expanding my own answer from the TBB forum:
You can use tbb::this_task_arena::max_concurrency() and tbb::this_task_arena::current_thread_index() to implement array-based custom thread local storage. The first function gives the upper limit for the number of working threads; to a degree it's TBB equivalent for omp_get_num_threads(). The second one gives an index of the current thread within the limit, similarly to omp_get_thread_num().
Ryan. Before suggesting something else, I would suggest you try to use enumerable_thread_specific if you can. It provides one feature you may have trouble getting in general: each variable is guaranteed to line up on a cache line, which eliminates false sharing.
If you decide to manage your own thread-local storage, you must
Allocate the storage
Assign the storage to a thread, and
(potentially) free the storage.
Remember also that TBB does not guarantee a particular number of threads, though in general it will give you what you ask for. Be careful of oversubscription.
You can use any storage that does not get reallocated, so std::vector<T> is out. I'd suggest you use a concurrent_vector<T>, which doesn't get moved on expanding the array.
So you have to assign each thread a slot in the vector. That index can be stored in TLS. Then use this index to fetch the instance from your concurrent_vector. This can be an expensive operation if the vector is fragmented.
You can also use the threadID of the thread to hash into storage. If you are willing to allocate a hash map once and never resize, this will work; otherwise you have to manage a chain of hash tables and walk through the chain looking for your instance. If I remember right enumerable_thread_specific uses this technique.
You can see it is a non-trivial to implement your own version, and you'll always do better if you use a stack variable in each thread and pass that as a formal parameter. Your problem may not be structured that way, though.

Why isn’t ReadOnlyDictionary thread-safe?

I’m looking for a readonly-dictionary to be accessed from multiple threads. While ConcurrentDictionary exposes such capabilities, I don’t want to have the overhead and the strange API.
.Net 4.5 while providing such a class, the documentation states that only static calls are safe.
I wonder why?
ReadOnlyDictionary is just a wrapper around any other dictionary. As such, it's only as thread-safe as the underlying dictionary.
In particular, if there's a thread modifying the underlying dictionary while another thread reads from the wrapper, there's no guarantee of safety.
If you want a ReadOnlyDictionary which is effectively immutable from all angles, you can create a clone of the original dictionary, create a ReadOnlyDictionary wrapper around that, and then not keep a reference to the clone anywhere. With only read operations going on, it should then be thread-safe. Of course, if the key or value types are mutable, that opens up a second degree of "thread-unsafety" to worry about.

Thread safe lazy construction of a singleton in C++

Is there a way to implement a singleton object in C++ that is:
Lazily constructed in a thread safe manner (two threads might simultaneously be the first user of the singleton - it should still only be constructed once).
Doesn't rely on static variables being constructed beforehand (so the singleton object is itself safe to use during the construction of static variables).
(I don't know my C++ well enough, but is it the case that integral and constant static variables are initialized before any code is executed (ie, even before static constructors are executed - their values may already be "initialized" in the program image)? If so - perhaps this can be exploited to implement a singleton mutex - which can in turn be used to guard the creation of the real singleton..)
Excellent, it seems that I have a couple of good answers now (shame I can't mark 2 or 3 as being the answer). There appears to be two broad solutions:
Use static initialisation (as opposed to dynamic initialisation) of a POD static variable, and implementing my own mutex with that using the builtin atomic instructions. This was the type of solution I was hinting at in my question, and I believe I knew already.
Use some other library function like pthread_once or boost::call_once. These I certainly didn't know about - and am very grateful for the answers posted.
Basically, you're asking for synchronized creation of a singleton, without using any synchronization (previously-constructed variables). In general, no, this is not possible. You need something available for synchronization.
As for your other question, yes, static variables which can be statically initialized (i.e. no runtime code necessary) are guaranteed to be initialized before other code is executed. This makes it possible to use a statically-initialized mutex to synchronize creation of the singleton.
From the 2003 revision of the C++ standard:
Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (3.9) with static storage duration initialized with constant expressions (5.19) shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit.
If you know that you will be using this singleton during the initialization of other static objects, I think you'll find that synchronization is a non-issue. To the best of my knowledge, all major compilers initialize static objects in a single thread, so thread-safety during static initialization. You can declare your singleton pointer to be NULL, and then check to see if it's been initialized before you use it.
However, this assumes that you know that you'll use this singleton during static initialization. This is also not guaranteed by the standard, so if you want to be completely safe, use a statically-initialized mutex.
Edit: Chris's suggestion to use an atomic compare-and-swap would certainly work. If portability is not an issue (and creating additional temporary singletons is not a problem), then it is a slightly lower overhead solution.
Unfortunately, Matt's answer features what's called double-checked locking which isn't supported by the C/C++ memory model. (It is supported by the Java 1.5 and later — and I think .NET — memory model.) This means that between the time when the pObj == NULL check takes place and when the lock (mutex) is acquired, pObj may have already been assigned on another thread. Thread switching happens whenever the OS wants it to, not between "lines" of a program (which have no meaning post-compilation in most languages).
Furthermore, as Matt acknowledges, he uses an int as a lock rather than an OS primitive. Don't do that. Proper locks require the use of memory barrier instructions, potentially cache-line flushes, and so on; use your operating system's primitives for locking. This is especially important because the primitives used can change between the individual CPU lines that your operating system runs on; what works on a CPU Foo might not work on CPU Foo2. Most operating systems either natively support POSIX threads (pthreads) or offer them as a wrapper for the OS threading package, so it's often best to illustrate examples using them.
If your operating system offers appropriate primitives, and if you absolutely need it for performance, instead of doing this type of locking/initialization you can use an atomic compare and swap operation to initialize a shared global variable. Essentially, what you write will look like this:
MySingleton *MySingleton::GetSingleton() {
if (pObj == NULL) {
// create a temporary instance of the singleton
MySingleton *temp = new MySingleton();
if (OSAtomicCompareAndSwapPtrBarrier(NULL, temp, &pObj) == false) {
// if the swap didn't take place, delete the temporary instance
delete temp;
}
}
return pObj;
}
This only works if it's safe to create multiple instances of your singleton (one per thread that happens to invoke GetSingleton() simultaneously), and then throw extras away. The OSAtomicCompareAndSwapPtrBarrier function provided on Mac OS X — most operating systems provide a similar primitive — checks whether pObj is NULL and only actually sets it to temp to it if it is. This uses hardware support to really, literally only perform the swap once and tell whether it happened.
Another facility to leverage if your OS offers it that's in between these two extremes is pthread_once. This lets you set up a function that's run only once - basically by doing all of the locking/barrier/etc. trickery for you - no matter how many times it's invoked or on how many threads it's invoked.
Here's a very simple lazily constructed singleton getter:
Singleton *Singleton::self() {
static Singleton instance;
return &instance;
}
This is lazy, and the next C++ standard (C++0x) requires it to be thread safe. In fact, I believe that at least g++ implements this in a thread safe manner. So if that's your target compiler or if you use a compiler which also implements this in a thread safe manner (maybe newer Visual Studio compilers do? I don't know), then this might be all you need.
Also see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2513.html on this topic.
You can't do it without any static variables, however if you are willing to tolerate one, you can use Boost.Thread for this purpose. Read the "one-time initialisation" section for more info.
Then in your singleton accessor function, use boost::call_once to construct the object, and return it.
For gcc, this is rather easy:
LazyType* GetMyLazyGlobal() {
static const LazyType* instance = new LazyType();
return instance;
}
GCC will make sure that the initialization is atomic. For VC++, this is not the case. :-(
One major issue with this mechanism is the lack of testability: if you need to reset the LazyType to a new one between tests, or want to change the LazyType* to a MockLazyType*, you won't be able to. Given this, it's usually best to use a static mutex + static pointer.
Also, possibly an aside: It's best to always avoid static non-POD types. (Pointers to PODs are OK.) The reasons for this are many: as you mention, initialization order isn't defined -- neither is the order in which destructors are called though. Because of this, programs will end up crashing when they try to exit; often not a big deal, but sometimes a showstopper when the profiler you are trying to use requires a clean exit.
While this question has already been answered, I think there are some other points to mention:
If you want lazy-instantiation of the singleton while using a pointer to a dynamically allocated instance, you'll have to make sure you clean it up at the right point.
You could use Matt's solution, but you'd need to use a proper mutex/critical section for locking, and by checking "pObj == NULL" both before and after the lock. Of course, pObj would also have to be static ;)
.
A mutex would be unnecessarily heavy in this case, you'd be better going with a critical section.
But as already stated, you can't guarantee threadsafe lazy-initialisation without using at least one synchronisation primitive.
Edit: Yup Derek, you're right. My bad. :)
You could use Matt's solution, but you'd need to use a proper mutex/critical section for locking, and by checking "pObj == NULL" both before and after the lock. Of course, pObj would also have to be static ;) . A mutex would be unnecessarily heavy in this case, you'd be better going with a critical section.
OJ, that doesn't work. As Chris pointed out, that's double-check locking, which is not guaranteed to work in the current C++ standard. See: C++ and the Perils of Double-Checked Locking
Edit: No problem, OJ. It's really nice in languages where it does work. I expect it will work in C++0x (though I'm not certain), because it's such a convenient idiom.
read on weak memory model. It can break double-checked locks and spinlocks. Intel is strong memory model (yet), so on Intel it's easier
carefully use "volatile" to avoid caching of parts the object in registers, otherwise you'll have initialized the object pointer, but not the object itself, and the other thread will crash
the order of static variables initialization versus shared code loading is sometimes not trivial. I've seen cases when the code to destruct an object was already unloaded, so the program crashed on exit
such objects are hard to destroy properly
In general singletons are hard to do right and hard to debug. It's better to avoid them altogether.
I suppose saying don't do this because it's not safe and will probably break more often than just initializing this stuff in main() isn't going to be that popular.
(And yes, I know that suggesting that means you shouldn't attempt to do interesting stuff in constructors of global objects. That's the point.)