Is SQLite dll thread safe?

Topics: General
Jan 17, 2012 at 4:48 PM
Edited Jan 17, 2012 at 4:49 PM

I need a DB in order to store GPS data in order to then plot it in the control.

So, i am thinking to reuse DLL for my own database.


Is it safe to use dll for internals (cache) and at the same time use it to interact with application database (GPS data)?


Thanks a lot



Env: windows xp+c# visual studio 10

Jan 17, 2012 at 5:05 PM

what do you mean reuse?

Jan 17, 2012 at 5:41 PM

Like launch a new thread listen seral port and insert data on the app database using the SQLite dll...

Is this possibble and thread-safe?



Jan 17, 2012 at 5:59 PM

why do you need to insert into the same database anyway? Just create fresh one and use it, inserting data to tile cache probably would slow down tile loading. And it's thread 'safe' as long as you use database connection on one thread, if you have more threads, you'll need more connections

Jan 17, 2012 at 6:16 PM

I think you did not understood:

the code/object uses SQLite database in order to implement map caching, right?

Now, i wat to use the map control in an aplication to plot veicle GPS information in the map control.

I am thinking t create a new database (veicule) and this database is updated by a thread that is listenning GSM modem on serial port. So, there are 2 databases: map caching and veicule. 

The question is: can thread use (is it safe) existing SQLite dll in order to write data to veicule database?

As i understood, if each thread has a separate connection there is not problem, right?





Jan 17, 2012 at 6:23 PM

sure, you can check demo source, there is ip address cache using sqlite, i guess it's something similar to your idea

p.s. set on sqlite reference option 'Copy to local' = False, if you have map in your app, you automatically will use internal dll

Jan 17, 2012 at 7:48 PM

Sorry, I thing you did not understood yet :-(

I dont want to implement nothing similar to the existing SQLite database(s) of the control.

Please, consider de demo application. It works very well and noting to say about it.


suppose now, that instead a button to create marks on demand, I wnat to draw marks in specific positions. Those possitions (veicules) are stored in a veicules.db at c:\ (this is my database of veicules and have nothing to do with demo sample)


So, I can reuse the SQLite dll (from the demo project) and use it in order to open veicules.db and load all veicules positions and draw them in map control as markers all this inside a new that that i have to create im demo project.

The question is: if the demo main thread yelds processor (or scheduler grabs it) and the the other thread (draw veicules) runs and both threads are using sqlite dll code, is it safe? (thread safe?)


Sorry but my eglish is not very good...



Jan 18, 2012 at 6:17 AM

one dll can use many programs, it has nothing to do with thread safeness anyway

Jan 18, 2012 at 9:13 AM
Edited Jan 18, 2012 at 9:15 AM

What i mean is one program (many threads) use one (the same) DLL!

If the dll code is not thread safe (with safe critical sections ) there will be problems...

Hence, I would like to know if the SQLite dll that come with control is thread safe or not.


Jan 18, 2012 at 10:37 AM

thread 'safeness' depends on your coding, not the dll

Jan 18, 2012 at 10:50 AM

There are 2 approachs:

IF dll is already safe, app code have no problem. If dll is not safe we have to take care of it at app code. As some app code is already written (the control) if dll is not safe i have to take care at my code (plot veicule on map) and i also have to protect control because now there are another thread that also uses the same dll.

It is my point of view.


But, no problem, the map control continues use the sqlite dll (for caching, etc) and I will implement sqlite veicule database connection by hand using C# suport for that. 



Jan 18, 2012 at 10:52 AM

you have strange idea, what is thread safe code... ;}

Jan 18, 2012 at 11:11 AM

Thread safe code is well written code that could be called "simultaneously" by 2 or more threads (or processes) and integrity is safeguarded.

Supopose a DLL x that implements funcion y.

Assume that function y uses global variables and is not protected

suppose now that 2 different threads call function y.

suppose that thread 1 is executing function y and at the middle the scheduler gives processor to thread 2 .

Suppose that thread 2 runts till the end of function y.

Then scheduler gives processor again to thread 1 (stopped at the middle)

When thread 1 runs again with have the global variables written by thread 2 and probably data integrity is compromised!


To handle that we used to define critical sections and semaphores (for exmple) in order to turn the function y thread safe. an improved way to turn function y thread safe is to rewrite it to just use local valiables (each thread have those lcoal variables on their stacks and there is no probem)




Jan 18, 2012 at 11:13 AM

dll can have N functions, half of them can be thread safe other half can be not thread safe, it has nothing to do with the dll itself ;}

Jan 18, 2012 at 12:24 PM

I quit! :-))


And i will write my own code for database connection and forget the existing dll