On Wed, September 20, 2006 04:39, Andreas Ntaflos wrote: > just a quick question or two regarding the use of libpqxx in a > multithreaded program. When using a peer-to-peer thread model [1] is it > safe to have each thread get a reference to the pqxx::connection C > created in the main thread? Or does each thread have to create its own > connection to the database? I suspect the latter is the case.
AFAICS there's nothing against the former, but: 1. You'll want to use a thread-safe version of libpq. 2. libpqxx doesn't do any locking, so you'll have to provide your own synchronization. It's safest to treat everything that's related to one connection as a single cluster of objects with a single lock. 3. Reference-counted objects (result, binarystring) are a bit tricky: if you copy-construct, assign, or destroy one you must be sure that no other copies of the same underlying object are being used at the same time, even though some of these operations are const. 4. Some things (e.g. pipelines, tablestreams) put a connection into a special mode where it can't deal with regular queries. If you share a connection between threads, and you use these special features, you'll have to make sure that no other thread (or even the same one!) tries to use the connection in the meantime. > I am quite new to multithreaded programming and still have to get used > to some concepts but as far as I understand the ACID principle no > concurrency issues should arise when reading from and writing to a > database like Postgres from multiple threads, and no mutexes are > required for such (but only such) operations either. Right. The reason why no concurrency issues arise is that a connection is essentially linear. Sometimes people try to open multiple simultaneous transactions on a single connection, but that's just not the way it works. For that you'd simply open multiple connections. Jeroen _______________________________________________ Libpqxx-general mailing list [email protected] http://gborg.postgresql.org/mailman/listinfo/libpqxx-general
