[go: up one dir, main page]

|
|
Log in / Subscribe / Register

DNF 3: better performance and a move to C++

DNF 3: better performance and a move to C++

Posted Mar 29, 2018 6:54 UTC (Thu) by rvfh (guest, #31018)
In reply to: DNF 3: better performance and a move to C++ by marcH
Parent article: DNF 3: better performance and a move to C++

I am using C++11 and have no malloc/free nor new/delete in my code at all. All is managed using either unique or shared pointers.
In the same way, C++ always to not lock/unlock a mutex, but take an object that locks at construction and unlocks at destruction.
This allows for rather safe (as in: no deadlock, no memory leak, no dangling pointers) code already!
And of course there is much more...


to post comments

DNF 3: better performance and a move to C++

Posted Mar 29, 2018 8:15 UTC (Thu) by andresfreund (subscriber, #69562) [Link] (1 responses)

How does this guarantee freedom of deadlocks?

DNF 3: better performance and a move to C++

Posted Mar 29, 2018 8:51 UTC (Thu) by rvfh (guest, #31018) [Link]

What it does guarantee is that you won't forget to unlock...

DNF 3: better performance and a move to C++

Posted Mar 29, 2018 11:58 UTC (Thu) by roc (subscriber, #30627) [Link] (2 responses)

C++ code is still vulnerable to dangling references even if it uses no raw pointers. I mentioned a string_view example up above. Other common bugs occur where you have getter methods returning references to class members; it's easy for these references to accidentally outlive the object.

Using shared pointers a lot can be really slow because of all the atomic operations you have to perform, even if you're traversing what should be a read-only data structure.

Using RAII to ensure mutex locks are paired with unlocks is a step up from raw pthread_mutex_lock. But how do you ensure that the right mutexes are held while you access the data they protect? Rust ensures this. C++ frameworks typically don't.

DNF 3: better performance and a move to C++

Posted Mar 29, 2018 15:13 UTC (Thu) by kpfleming (subscriber, #23250) [Link] (1 responses)

Unfortunately the example you posted above did in fact use a raw pointer, it was just cleverly disguised :-)

DNF 3: better performance and a move to C++

Posted Mar 29, 2018 21:22 UTC (Thu) by roc (subscriber, #30627) [Link]

Well sure, but once you admit that references and 'this' are just cleverly disguised raw pointers, you have no hope of getting away from them in C++.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds