How to ensure thread-safety

James Aylett james-xapian at tartarus.org
Fri Feb 9 19:33:20 GMT 2018


On 8 Feb 2018, at 15:18, Kim Walisch <kim.walisch at gmail.com> wrote:

> I have read the concurrency webpage from the Xapian documentation:
> 
> http://getting-started-with-xapian.readthedocs.io/en/latest/concepts/concurrency.html
> 
> But it is still not clear to me how to ensure thread-safety when using
> libxapian (C++ API).
> 
> 1) Can many thread search the
> database provided each thread uses their own Xapian database object and
> none of the threads updates/indexes the database?

This is covered explicitly in the document you linked to:

> Xapian doesn’t maintain any global state, so you can safely use Xapian in a multi-threaded program provided you don’t share objects between threads. In practice this restriction is often not a problem - each thread can create its own xapian.Database object, and everything will work fine.


So the answer is 'yes'.

> 2) What about when one thread indexes a new document? Do I need to
> ensure (by locking) that no other thread reads or write the database until
> the first thread has finished indexing?

This isn't a problem either; Xapian takes care of lock management for you. It's covered in a different part of the manual: https://getting-started-with-xapian.readthedocs.io/en/latest/concepts/indexing/databases.html#concurrent-access

> Currently, all the backends only support a single writer existing at a given time; attempting to open another writer on the same database will throw xapian.DatabaseLockError to indicate that it wasn’t possible to acquire a lock. Multiple concurrent readers are supported (in addition to the writer).


Note the caution there about the number of versions that can exist in Xapian's MVCC, and how to deal with DatabaseModifiedError.

J

-- 
 James Aylett, occasional troublemaker & project governance
 xapian.org




More information about the Xapian-discuss mailing list