简体   繁体   English

C多线程-通过2个线程崩溃Sqlite3数据库访问

[英]C Multithreading - Sqlite3 database access by 2 threads crash

Here is a description of my problem: 这是我的问题的描述:

I have 2 threads in my program. 我的程序中有2个线程。 One is the main thread and the other one that i create using pthread_create 一个是主线程,另一个是我使用pthread_create创建的线程

The main thread performs various functions on an sqlite3 database . 主线程在sqlite3数据库上执行各种功能。 Each function opens to perform the required actions and closing it when done. 每个功能都会打开以执行所需的操作,并在完成后将其关闭。

The other thread simply reads from the database after a set interval of time and uploads it onto a server. 另一个线程在设置的时间间隔后简单地从数据库读取,并将其上载到服务器。 The thread also opens and closes the database to perform its operation. 该线程还打开和关闭数据库以执行其操作。

The problem occurs when both threads happen to open the database. 当两个线程碰巧都打开数据库时,会发生此问题。 If one finishes first, it closes the database thus causing the other to crash making the application unusable. 如果第一个完成,它将关闭数据库,从而导致另一个崩溃,使应用程序无法使用。 Main requires the database for every operation. Main每次操作都需要数据库。

Is there a way I can prevent this from happening? 有什么方法可以防止这种情况发生吗? Mutex is one way but if I use mutex it will make my main thread useless. 互斥锁是一种方法,但是如果我使用互斥锁,它将使我的主线程无用。 Main thread must remain functional at all times and the other thread runs in the background. 主线程必须始终保持功能正常,而其他线程则在后台运行。

Any advice to make this work would be great. 任何建议,使这项工作将是巨大的。 I did not provide snippets as this problem is a bit too vast for that but if you do not understand anything about the problem please do let me know. 我没有提供摘要,因为这个问题太大了,但是如果您对这个问题不了解,请告诉我。

EDIT: 编辑:

static sqlite3 *db = NULL;

Code snippet for opening database 用于打开数据库的代码段

int open_database(char* DB_dir) // argument is the db path
        rc = sqlite3_open(DB_dir , &db); 

        if( rc )                
        {
//failed to open message
            sqlite3_close(db); 
            db = NULL;
            return SDK_SQL_ERR;
        }
        else
        {
            //success message
        }
    }
    return SDK_OK;

}

And to close db 并关闭db

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

EDIT: I forgot to add that the background thread performs one single write operation that updates 1 field of the table for each row it uploads onto the server 编辑:我忘了补充说,后台线程执行一个写入操作,该操作为表上载到服务器的每一行更新表的1个字段

Have your threads each use their own database connection. 让您的线程各自使用自己的数据库连接。 There's no reason for the background thread to affect the main thread's connection. 后台线程没有理由影响主线程的连接。

Generally, I would want to be using connection pooling, so that I don't open and close database connections very frequently; 通常,我想使用连接池,这样我就不会非常频繁地打开和关闭数据库连接。 connection opening is an expensive operation. 连接打开是一项昂贵的操作。

In application servers we very often have many threads, we find that a connection pool of a few tens of connections is sufficient to service requests on behalf of many hundreds of users. 在应用服务器中,我们经常有很多线程,我们发现只有几十个连接的连接池足以代表数百个用户处理请求。

Basically built into sqlite3 there are mechanisms to provide locking... BEGIN EXCLUSIVE then you can also register a sleep callback so that the other thread can do other things... 基本内置于sqlite3中的机制提供锁定... BEGIN EXCLUSIVE然后您还可以注册一个sleep回调,以便其他线程可以执行其他操作...

see sqlite3_busy_handler() 参见sqlite3_busy_handler()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM