libmx3 / mx3

a sample project showcasing/collecting cross platform techniques on mobile
MIT License
1.18k stars 146 forks source link

Question about "this isn't thread safe" comment #67

Closed kabram closed 9 years ago

kabram commented 9 years ago

Still working through understanding all that is going on in the examples and came across a comment "// todo(kabbes) this isn't thread safe" in UserListVmHandle::_notify_new_data() Could anyone shed light on what is not thread safe here?

skabbes commented 9 years ago

In that class m_monitor doesn't make any guarantees about what thread it calls its listen_to_changes callback on. Therefore, _notify_new_data could be accessing the database from a thread concurrent with the background thread.

skabbes commented 9 years ago

The corresponding TODO is here:

https://github.com/libmx3/mx3/blob/master/src/sqlite_query/query_monitor.cpp#L56-L62

kabram commented 9 years ago

Ah I missed that comment, Thanks. I noticed that the db is opened using NOMUTEX (multithreaded mode), from what I understand in the SQLite documentation opening with the FULLMUTEX (serial mode) would mean that SQLite handles all the locking needed when accessing to the db. Either that or as per your comment you would have to have a serial queue to handle all access to the db. Does anyone have any experience or insight into using these different methods for db access?

skabbes commented 9 years ago

I've noticed that the event loop / serial queue model is "winning", at least on mobile development. What is certainly missing in this repo, is a good standard base library to build these kinds of things on top of.

My personal gripe with sqlite FULLMUTEX is that you almost certainly need to do your own locking on top of that in the layers that access it. Serial queues are pretty intrinsic to mobile or just UI development in general, so I think it is nice to match that. I am one of the lead developers on Mailbox and this model has worked very well for us across iOS, Android and Mac. Dropbox does an interesting hybrid between both models (synchronous, and queued) but the momentum inside the company is definitely leaning towards a serial queue / event loop / task runner model.

Here's a good video to learn about it: http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Herb-Sutter-Concurrency-and-Parallelism