Hi,

I'm using an event loop type system which I've implemented myself. This is due to specific needs.

Event loop is fairly simple, it has several types of events:
- Timer based events (relative or exact, repeating, etc)
- IO based events
- Notification based events

Notifications are events which can come from any thread, and get processed on in the main loop.

Here is the function which is used to post a notification. REF(t) is simply a macro for boost::intrusive_ptr<t>.

void Loop::postNotification (REF(INotificationSource) note, bool urgent)
{
using namespace boost;
typedef boost::mutex::scoped_lock scoped_lock;


// Lock the event loop notification queue
// Add note to the end of the queue
// Interrupt event loop thread if urgent


// We should probably lock around m_currentThread, it might not be set before we read it..?
if (this_thread::get_id() == m_currentThread) {
note->processEvents(this, NOTIFICATION);
} else {
{
// Enqueue the notification to be processed
scoped_lock lock(m_notifications.lock);
m_notifications.sources->push(note); (*)
}


if (urgent) {
// Interrupt event loop thread so that it processes notifications more quickly
m_urgentNotificationPipe->notifyEventLoop();
}
}
}

I'm experiencing a very infrequent crash at the line marked with (*). The note is a reference counted pointer (boost::intrusive_ptr), but by the time we get here, this pointer is invalid, even though there are still references to it in the system. The odd thing is, further up the chain (and on the same thread), I'm checking to ensure that note is valid, i.e.

//... display update system, separate thread from event-loop.
ensure(ctx->notificationSource);
loop->postNotification(ctx->notificationSource);
//...

After writing this far in the email, I'm starting to see something where there could be an error. Is it possible that the object reference count is being messed up due to multi-threading issues? I.e. if the object is being passed around on two separate threads, having the reference count incremented and decremented at the same time? Actually, it seems obvious that this could be a problem...

Are there any "patterns" for using reference counted objects across different threads?

Any feedback is appreciated.

Kind regards,
Samuel