Articles contenant le tag C++
C++ Thread synchronization pitfall: using a barrier to synchronize a thread start
Posté par Olivier dans Trucs & Astuces le 20 novembre 2013
The context: You want to create a worker thread. Before the main thread goes on, you want to ensure that the worker thread starts. Using a boost::barrier seems like a good idea.
std::unique_ptr, virtual and missing virtual destructor = major pitfall
Posté par Olivier dans Trucs & Astuces le 29 août 2013
In our company, the build infrastructure runs unit tests in a valgrind shell, trying to detect memory leaks at the time the unit tests are executed. And every now and then, although our memory allocations are mostly handled through std::unique_ptr or std::shared_ptr, a leak pops up on the radar. And usually with the most useless […]
Why does std::for_each return the Functor ?
Posté par Olivier dans Trucs & Astuces le 9 décembre 2010
In short: Because the provided Functor is copied by for_each. A bit longer: the functor has to be copied so that for_each can use a reference to it in the loopage as the functor is copied, the context of the functor would be lost at end of for_each if it was not returned class Object […]
Bad idea: get a read lock while holding a write lock
Posté par Olivier dans Trucs & Astuces le 4 novembre 2009
Under Linux, the following code hangs in the second execution of the loop at the time the write lock is acquired. pthread_rwlock_t lock; fprintf(stderr, « Init\n »); pthread_rwlock_init(&lock, NULL); for(int i = 0; i < 2; ++i) { fprintf(stderr, « Get Write lock\n »); pthread_rwlock_wrlock(&lock); fprintf(stderr, « Get Read lock\n »); pthread_rwlock_rdlock(&lock); fprintf(stderr, « Release Read lock\n »); pthread_rwlock_unlock(&lock); fprintf(stderr, « Release Write lock\n »); […]