raii

Locking a mutex in a destructor in C++11

佐手、 提交于 2019-12-03 00:36:17
I have some code which need to be thread safe and exception safe. The code below is a very simplified version of my problem : #include <mutex> #include <thread> std::mutex mutex; int n=0; class Counter{ public: Counter(){ std::lock_guard<std::mutex>guard(mutex); n++;} ~Counter(){ std::lock_guard<std::mutex>guard(mutex);//How can I protect here the underlying code to mutex.lock() ? n--;} }; void doSomething(){ Counter counter; //Here I could do something meaningful } int numberOfThreadInDoSomething(){ std::lock_guard<std::mutex>guard(mutex); return n;} I have a mutex that I need to lock in the

Managing objective-C objects with c++ std::unique_ptr<> or std::shared_ptr<>

自古美人都是妖i 提交于 2019-12-03 00:23:02
Objective-C can be mixed with c++ to some extent and can be called to each other . But Objective-C objects still are more or less manually managed, and RAII idiom is entirely absent from the language. I wonder if it is possible to manage the lifetimes of Objective-C objects with c++ smart pointers. Specially now that both boost scoped_ptr and shared_ptr have been added to the C++11 standard But Objective-C objects still are more or less manually managed, and RAII idiom is entirely absent from the language. I think this would seem to answer your question anyhow. Because Objective-C objects are

custom RAII C++ implementation for scoped mutex locks

心不动则不痛 提交于 2019-12-02 22:16:22
I cannot use boost or the latest std::thread library. The way to go is to create a custom implementation of a scoped mutex. In a few words when a class instance is create a mutex locks. Upon class destruction the mutex is unlocked. Any implementation available? I don't want to re-invent the wheel. I need to use pthreads. resource acquisition is initialization == “RAII” Note This is an old answer. C++11 contains better helpers that are more platform independent: std::lock_guard std::mutex , std::timed_mutex , std::recursive_mutex , std::recursive_timed_mutex And other options like std::unique

Why garbage collection when RAII is available?

此生再无相见时 提交于 2019-12-02 18:45:57
I hear talks of C++14 introducing a garbage collector in the C++ standard library itself. What is the rationale behind this feature? Isn't this the reason that RAII exists in C++? How will the presence of standard library garbage collector affect the RAII semantic? How does it matter to me(the programmer) or the way in which I write C++ programs? Ali Garbage collection and RAII are useful in different contexts. The presence of GC should not affect your use of RAII. Since RAII is well-known, I give two examples where GC is handy. Garbage collection would be a great help in implementing lock

RAII vs. exceptions

扶醉桌前 提交于 2019-12-02 14:04:26
The more we use RAII in C++, the more we find ourselves with destructors that do non-trivial deallocation. Now, deallocation (finalization, however you want to call it) can fail, in which case exceptions are really the only way to let anybody upstairs know of our deallocation problem. But then again, throwing-destructors are a bad idea because of the possibility of exceptions being thrown during stack unwinding. std::uncaught_exception() lets you know when that happens, but not much more, so aside from letting you log a message before termination there's not much you can do, unless you're

call function when leaving scope

无人久伴 提交于 2019-12-02 06:58:42
What is the most elegant solution for calling a function automatically when leaving a scope? My current approach (see below) works but I guess there should be something more general as writing a custom class for this. #include <iostream> #include <functional> using namespace std; class DoInDtor { public: typedef function<void()> F; DoInDtor(F f) : f_(f) {}; ~DoInDtor() { f_(); } private: F f_; }; void foo() { DoInDtor byeSayerCustom([](){ cout << "bye\n"; }); auto cond = true; // could of course also be false if ( cond ) return; return; } int main() { foo(); } Sure, one could abuse std::unique

How do I run a cleanup code on the function exit?

安稳与你 提交于 2019-12-02 06:44:33
C++ classes provide RAII idiom. Therefore you don't have to care about exceptions: void function() { // The memory will be freed automatically on function exit std::vector<int> vector(1000); // Do some work } But if you have (for some reasons) to use some pure C API, you have either to create C++ wrappers around it or to use try/catch blocks void function() { int *arr = (int*)malloc(1000*sizeof(int)); if (!arr) { throw "cannot malloc"; } try { // Do some work } catch (...) { free(arr); // Free memory in case of exception throw; // Rethrow the exception } // Free memory in case of success free

Fastest `finally` for C++ [closed]

我与影子孤独终老i 提交于 2019-12-01 04:08:37
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . C++ so far (unfortunately) doesn't support finally clause for a try statement. This leads to speculations on how to release resources. After studying the question on the internet, although I found some solutions, I didn't get clear about their performance (and I would use Java if performance didn't

Any RAII template in boost or C++0x

£可爱£侵袭症+ 提交于 2019-11-30 17:33:42
Is there any template available in boost for RAII . There are classes like scoped_ptr , shared_ptr which basically work on pointer. Can those classes be used for any other resources other than pointers. Is there any template which works with a general resources. Take for example some resource which is acquired in the beginning of a scope and has to be somehow released at the end of scope. Both acquire and release take some steps. We could write a template which takes two(or maybe one object) functors which do this task. I havent thought it through how this can be achieved, i was just wondering

Preventing users from creating unnamed instances of a class [duplicate]

无人久伴 提交于 2019-11-30 17:17:17
This question already has an answer here: How to avoid C++ anonymous objects 6 answers For many RAII "guard" classes, being instantiated as anonymous variables does not make sense at all: { std::lock_guard<std::mutex>{some_mutex}; // Does not protect the scope! // The unnamed instance is immediately destroyed. } { scope_guard{[]{ cleanup(); }}; // `cleanup()` is executed immediately! // The unnamed instance is immediately destroyed. } From this article : Anonymous variables in C++ have “expression scope”, meaning they are destroyed at the end of the expression in which they are created. Is