Multithreading with same sqlite struct

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Multithreading with same sqlite struct

Brown, Dave

I read the docs on thread safety, where it says:

"Threadsafe" in the previous paragraph means that two or more threads can
run SQLite at the same time on different "sqlite" structures returned from
separate calls to sqlite_open(). It is never safe to use the same sqlite
structure pointer simultaneously in two or more threads.

QUESTION: If I wrap the database calls with a mutex, so two threads can't
use the sqlite structure simultaneously, is this ok?  (Even though a thread
which didn't create the struct will be using it).

-Dave
Reply | Threaded
Open this post in threaded view
|

Re: Multithreading with same sqlite struct

Cory Nelson
Yep, that's how I do it.  Works fine.

On 6/10/05, Brown, Dave <[hidden email]> wrote:

>
> I read the docs on thread safety, where it says:
>
> "Threadsafe" in the previous paragraph means that two or more threads can
> run SQLite at the same time on different "sqlite" structures returned from
> separate calls to sqlite_open(). It is never safe to use the same sqlite
> structure pointer simultaneously in two or more threads.
>
> QUESTION: If I wrap the database calls with a mutex, so two threads can't
> use the sqlite structure simultaneously, is this ok?  (Even though a thread
> which didn't create the struct will be using it).
>
> -Dave
>


--
Cory Nelson
http://www.int64.org
Reply | Threaded
Open this post in threaded view
|

Re: Multithreading with same sqlite struct

Dan Kennedy
It's not recommended. Some operating systems (notably linux), have
problems releasing file-locks established by other threads.

You might be Ok on Windows, I'm not sure. But be careful, the
database locking might not work.

--- Cory Nelson <[hidden email]> wrote:

> Yep, that's how I do it.  Works fine.
>
> On 6/10/05, Brown, Dave <[hidden email]> wrote:
> >
> > I read the docs on thread safety, where it says:
> >
> > "Threadsafe" in the previous paragraph means that two or more threads can
> > run SQLite at the same time on different "sqlite" structures returned from
> > separate calls to sqlite_open(). It is never safe to use the same sqlite
> > structure pointer simultaneously in two or more threads.
> >
> > QUESTION: If I wrap the database calls with a mutex, so two threads can't
> > use the sqlite structure simultaneously, is this ok?  (Even though a thread
> > which didn't create the struct will be using it).
> >
> > -Dave
> >
>
>
> --
> Cory Nelson
> http://www.int64.org
>



               
__________________________________
Discover Yahoo!
Find restaurants, movies, travel and more fun for the weekend. Check it out!
http://discover.yahoo.com/weekend.html 

Reply | Threaded
Open this post in threaded view
|

Re: Multithreading with same sqlite struct

Christopher R. Palmer
Dan Kennedy wrote:
> It's not recommended. Some operating systems (notably linux), have
> problems releasing file-locks established by other threads.
>
> You might be Ok on Windows, I'm not sure. But be careful, the
> database locking might not work.

Actually, I would say that you're probably better off wrapping the calls
with a mutex.  Just make sure that a transaction is started and completed
by the same thread.  I say you're better off because there appears to be
problems with the file locking for multiple threads:

http://www.sqlite.org/cvstrac/tktview?tn=1272

Cheers,
Chris.

> --- Cory Nelson <[hidden email]> wrote:
>
>
>>Yep, that's how I do it.  Works fine.
>>
>>On 6/10/05, Brown, Dave <[hidden email]> wrote:
>>
>>>I read the docs on thread safety, where it says:
>>>
>>>"Threadsafe" in the previous paragraph means that two or more threads can
>>>run SQLite at the same time on different "sqlite" structures returned from
>>>separate calls to sqlite_open(). It is never safe to use the same sqlite
>>>structure pointer simultaneously in two or more threads.
>>>
>>>QUESTION: If I wrap the database calls with a mutex, so two threads can't
>>>use the sqlite structure simultaneously, is this ok?  (Even though a thread
>>>which didn't create the struct will be using it).
>>>
>>>-Dave
>>>
>>
>>
>>--
>>Cory Nelson
>>http://www.int64.org
>>
>
>
>
>
>
> __________________________________
> Discover Yahoo!
> Find restaurants, movies, travel and more fun for the weekend. Check it out!
> http://discover.yahoo.com/weekend.html