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

make virtually any pthread_mutex_t implementation POSIX-Safe, wrt. waiters and lock dtor...

7 views
Skip to first unread message

Chris Thomasson

unread,
Oct 24, 2006, 3:38:59 AM10/24/06
to
First, wrap the pthread mutex in a C++ wrapper or something:


class mutex {
// yeah, yeah, lock and unlock guards too...
};

Now you can do stuff like this, and not have to worry about the
race-condition in POSIX:

http://groups.google.com/group/comp.programming.threads/msg/667fa209809b41db
(refer to the last two paragraphs...)


class myobj {

// whatever...


Just declare a couple of atomic pointer types to your class; you can use
this one if you want:

http://appcore.home.comcast.net/vzoom/refcount/

public:
typedef vzsync::ptr::global<myobj > ptr_global_t;
typedef vzsync::ptr::local<myobj > ptr_local_t;

And add a mutex:

mutex m_mutex;
unsigned m_flags; // protected by m_mutex
};


static myobj::ptr_global_t g_obj;


voit threads_a_through_d(...) {
for(;;) {
// do whatever...

// decides to access g_obj {
myobj::ptr_local_t obj(g_obj);
if (obj) {
mutex::guard_t lock(obj->m_mutex);
// do what you need to do...
}
}

// do whatever......

// do whatever.........
}
}


#define REMOVE_AND_REPLACE_FLAG() 0x8000


voit threads_e_through_j(...) {
for(;;) {
// do whatever...

// decides to access g_obj {
myobj::ptr_local_t obj(g_obj);
if (obj) {
unsigned flags;
{
mutex::guard_t lock(obj->m_mutex);
// do what you need to do...

// reads and decides to do something based on flags
flags = obj->m_flags;
}

if (flags & REMOVE_AND_REPLACE_FLAG()) {
myobj::ptr_local_t new_obj(new myobj(flags));
g_obj = new_obj;
}
}
}

// do whatever......

// do whatever.........
}
}

;^)


0 new messages