[odb-users] query crash

Boris Kolpackov boris at codesynthesis.com
Fri Nov 3 05:23:42 EDT 2017


Патрушев Данил Андреевич <d.patrushev at prosoftsystems.ru> writes:

> We have a single instance of odb::pgsql::database. We have a custom
> connection pool, all it does is override the "create" method which
> creates a connection with the socket options that we need.
> 
> 1) odb::query<MYTABLE> q(MYTABLE::rowid == 555); // create query in main thread
>  
> 2) auto functor = [q](){//copy query into the lambda
> odb::database* db = database(); //single instance
> odb::transaction tran(db->begin());
> auto result = db->query<MYTABLE>(q);
> //iterate over result and construct a list of db objects;
> tran.commit();
> return list_of_db_objects;
> };
> 
> See, by result I mean a list of objects extracted from odb::result<MYTABLE>;
> 
> 3) auto future = QtConcurrent::run(functor); //(I quess you are familiar with the Qt framework)
>    We launch a worker thread and wait for the finished signal (the original
>    query gets destroyed along the way)
> 
> 4) The functor gets executed in the worker thread's context. After the
>    finished signal is emitted, we extract the result from the future by
>    calling future->result() in main thread;
> 
> I quess that if odb::pgsql::query class is not thread-safe, then we are
> wrong to use it this way .

No, this should work provided you don't have by-reference parameters
in your query which you initialize concurrently from multiple threads.
Quoting the manual, Section 4.3, "Executing a Query":

"A named query instance that does not have any by-reference parameters is
immutable and can be shared between multiple threads without
synchronization. On the other hand, a query instance with by-reference
parameters is modified every time it is executed. If such a query is shared
among multiple threads, then access to this query instance must be
synchronized from the execution point and until the completion of the
iteration over the result."

If this is not the issue, can you show the stack trace?

Boris



More information about the odb-users mailing list