日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > > 充電吧
[導(dǎo)讀]作者:朱金燦來源:http://blog.csdn.net/clever101??? ??????? 在并發(fā)訪問sqlite數(shù)據(jù)庫會出現(xiàn)這樣一個錯誤:databseis locked,這是sqlite

作者:朱金燦

來源:http://blog.csdn.net/clever101

???

??????? 在并發(fā)訪問sqlite數(shù)據(jù)庫會出現(xiàn)這樣一個錯誤:databseis locked,這是sqlite數(shù)據(jù)庫對并發(fā)支持不太好的緣故造成的。采用網(wǎng)上的一種的思路是通過互斥信號量來達(dá)到并發(fā)訪問的目的。下面是一個跨平臺的互斥信號量類:


//ProcessMutex.h文件: 
#ifndef __PROCESS_MUTEX_H__
#define __PROCESS_MUTEX_H__
 
#if defined _WIN32 || defined _WIN64

     #include 

#endif
 
#ifdef linux
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#endif
 
class CProcessMutex
{
public:
    /* 默認(rèn)創(chuàng)建匿名的互斥 */
    CProcessMutex(const char* name = NULL);
    ~CProcessMutex();
 
    bool Lock();
    bool UnLock();
private:

#if defined _WIN32 || defined _WIN64

    void* m_pMutex;

#endif

#ifdef linux
    set_t* m_pSem;
#ednif
    char m_cMutexName[30];
};
#endif

 //ProcessMutex.cpp文件:
#include "ProcessMutex.h"
 
#if defined _WIN32 || defined _WIN64
 
CProcessMutex::CProcessMutex(const char* name)
{
    memset(m_cMutexName, 0 ,sizeof(m_cMutexName));
    int min = strlen(name)>(sizeof(m_cMutexName)-1)?(sizeof(m_cMutexName)-1):strlen(name);
    strncpy(m_cMutexName, name, min);
    m_pMutex = CreateMutex(NULL, false, m_cMutexName);
}
 
CProcessMutex::~CProcessMutex()
{
    CloseHandle(m_pMutex);
}
 
bool CProcessMutex::Lock()
{
    //互斥鎖創(chuàng)建失敗
    if (NULL == m_pMutex)
    {
        return false;
    }
     
    DWORD nRet = WaitForSingleObject(m_pMutex, INFINITE);
    if (nRet != WAIT_OBJECT_0)
    {
        return false;
    }
 
    return true;
}
 
bool CProcessMutex::UnLock()
{
    return ReleaseMutex(m_pMutex);
}
 
#endif
 
#ifdef linux
 
CProcessMutex::CProcessMutex(const char* name)
{
    memset(m_cMutexName, 0 ,sizeof(m_cMutexName));
    int min = strlen(name)>(sizeof(m_cMutexName)-1)?(sizeof(m_cMutexName)-1):strlen(name);
    strncpy(m_cMutexName, name, min);
    m_pSem = sem_open(name, O_CREAT, 0644, 1);
}
 
CProcessMutex::~CProcessMutex()
{
    int ret = sem_close(m_pSem);
    if (0 != ret)
    {
        printf("sem_close error %dn", ret);
    }
    sem_unlink(m_cMutexName);
}
 
bool CProcessMutex::Lock()
{
    int ret = sem_wait(m_pSem);
    if (ret != 0)
    {
        return false;
    }
    return true;
}
 
bool CProcessMutex::UnLock()
{
    int ret = sem_post(m_pSem);
    if (ret != 0)
    {
        return false;
    }
    return true;
}
 
#endif

使用示例代碼如下:

CProcessMutex pMutex("MutexName");

pMutex.Lock();

sqlite3_exec( myconn, sql, 0, 0, &m_sqlerr_msg); // 執(zhí)行sqlite的相關(guān)操作

pMutex.UnLock();

參考文獻(xiàn):

?

1. 多進(jìn)程之間的互斥信號量的實(shí)現(xiàn)(Linux和windows跨平臺)

?

2.sqlite遇到database is locked問題的完美解決

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

  醫(yī)療解決方案目的的臨界性是免除干擾影響的重要因素。雖然無線局域網(wǎng)絡(luò)、藍(lán)牙低功耗技術(shù)等無線接口本身可用于較大規(guī)模的網(wǎng)絡(luò)運(yùn)作,而且該項(xiàng)低功耗技術(shù)能改善原本藍(lán)牙技術(shù)所采用的多項(xiàng)組件,然而,裝置操作環(huán)境中

關(guān)鍵字: Android 數(shù)據(jù)庫 游戲開發(fā) sqlite

作為針對Red Hat Enterprise Linux的最后一步,F(xiàn)edora的Red Hat開發(fā)人員計劃將以前使用的Berkeley DB RPM數(shù)據(jù)庫(RPMDB)切換到SQLite。

關(guān)鍵字: berkeley Linux sqlite rpm數(shù)據(jù)庫

原文轉(zhuǎn)載于:https://www.cnblogs.com/5211314jackrose/p/5816066.html1、異步I/O模式?? ?通常,當(dāng)SQLite寫一個數(shù)據(jù)庫文件時,會等待,直到寫

關(guān)鍵字: sqlite

Singleton 單例模式,又叫單子模式,是一種常見的軟件設(shè)計模式。這種模式的特點(diǎn)就是應(yīng)用了?Singleton 單例模式的類必須保證始終只有一個實(shí)例(對象)存在。許多時候系統(tǒng)中需要某個類只能同時存

關(guān)鍵字: sqlite

????今天在Qt for Android跑離線數(shù)據(jù),要使用到Sqlite數(shù)據(jù)庫,當(dāng)時是在pc端跑過了的,一切流程都很ok了,所以就準(zhǔn)備轉(zhuǎn)移到安卓設(shè)備上面試一試,發(fā)現(xiàn)剛運(yùn)行初始化Sqlite時就出現(xiàn)了

關(guān)鍵字: qml sqlite localstorage

* 從 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 下載了 sqlite-netFx35-setup-b

關(guān)鍵字: c# sqlite

SQLite 日期類型(轉(zhuǎn))SQLite日期類型簡單示例:SELECT???? datetime(CHANGE_DATE,'localtime'),???? strftime('%Y-%m-%d',C

關(guān)鍵字: sqlite 日期類型

SQLite說C語言好,到底好在哪里?

關(guān)鍵字: C語言 sqlite

打開sqlite數(shù)據(jù)庫需要用到sqlite3_open函數(shù),但是sqlite3_open函數(shù)的第一個參數(shù)是數(shù)據(jù)庫文件的絕對路徑。它是有講究的,必須是utf8字符串。也就是說假如文件路徑是非utf8字符

關(guān)鍵字: sqlite

目錄(?)[+]一、初識sqlite?????????偶然的機(jī)會接觸到sqlite,不禁驚嘆sqlite的體型小巧而功能強(qiáng)大(看來軟件也不可貌相哦),Sqlite 是開源的內(nèi)存數(shù)據(jù)庫(也可以稱之為內(nèi)嵌

關(guān)鍵字: sqlite 數(shù)據(jù)庫建立
關(guān)閉