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

raii can be helpful...?

Skip to first unread message

Chris M. Thomasson

Dec 2, 2023, 11:21:19 PM12/2/23
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:

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

// a, x, and b are all locked!

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