C Multithreading - Sqlite3 database access by 2 threads crash -


here description of problem:

i have 2 threads in program. 1 main thread , other 1 create using pthread_create

the main thread performs various functions on sqlite3 database. each function opens perform required actions , closing when done.

the other thread reads database after set interval of time , uploads onto server. thread opens , closes database perform operation.

the problem occurs when both threads happen open database. if 1 finishes first, closes database causing other crash making application unusable. main requires database every operation.

is there way can prevent happening? mutex 1 way if use mutex make main thread useless. main thread must remain functional @ times , other thread runs in background.

any advice make work great. did not provide snippets problem bit vast if not understand problem please let me know.

edit:

static sqlite3 *db = null; 

code snippet opening database

int open_database(char* db_dir) // argument db path         rc = sqlite3_open(db_dir , &db);           if( rc )                         { //failed open message             sqlite3_close(db);              db = null;             return sdk_sql_err;         }         else         {             //success message         }     }     return sdk_ok;  } 

and close db

int close_database() {     if(db!=null)     {         sqlite3_close(db);         db = null;         //success message     }     return 1; } 

edit: forgot add background thread performs 1 single write operation updates 1 field of table each row uploads onto server

have threads each use own database connection. there's no reason background thread affect main thread's connection.

generally, want using connection pooling, don't open , close database connections frequently; connection opening expensive operation.

in application servers have many threads, find connection pool of few tens of connections sufficient service requests on behalf of many hundreds of users.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -