Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

raii can be helpful...?

27 views
Skip to first unread message

Chris M. Thomasson

unread,
Dec 2, 2023, 11:21:19 PM12/2/23
to
Just in case I need to do any locking in my new project... I remember
this older work... Take notice of the raii based locker object in:

https://groups.google.com/g/comp.lang.c++/c/sV4WC_cBb9Q/m/SkSqpSxGCAAJ

locker is meant to be on a per-thread basis. The view into a threads
goal of atomic, deadlock free, wrt its locked objects... Is this crap
wrt trying to show how raii can help, wrt the locker object?

____________
This code allows a thread to dynamically lock multiple addresses in a
totally atomic fashion, and free of deadlock. It is based on hashing
pointers into a table of locks. A thread can create a little array of
hashed pointers, sort them, remove duplicates, then take all of the
locks in the table. The sorting and uniqueness factor prevents deadlock.
_________________
// pseudo-code

mtx_table lock_table;


// some vars
int a = 3;
short b = 6;
double x = 1.234;

// a thread
{
locker locks(lock_table); // our hashed locks
locks.push(&a);
locks.push(&x);
locks.push(&b);

locks.lock();
// a, x, and b are all locked!
locks.unlock();
}
_________________


Well, locks.lock() and locks.unlock() can be automated via raii here...
locker is nice...
0 new messages