SQLite multiple threads
const int loops = 1000; public void DatabaseThreadSafetyTest()
{
var backgroundThread = new Thread(new System.Threading.ThreadStart(() =>
{
for (int i = 1; i <= loops; i++)
{
Console.WriteLine("Background thread loop " + i);
using (var db = new SQLiteConnection(DbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache)) {
db.Insert (new MyClass());
}
}
}));
backgroundThread.Start(); for (int i = 1; i <= loops; i++)
{
Console.WriteLine("Main thread loop " + i);
using (var db = new SQLiteConnection(DbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache)) {
db.Insert (new MyClass());
}
}
}
using System;
using System.Data.SQLite;
using System.Threading.Tasks; namespace SQLiteTest
{
class Program
{
static void Main(string[] args)
{
var tasks = new Task[100]; for (int i = 0; i < 100; i++)
{
tasks[i] = new Task(new Program().WriteToDB);
tasks[i].Start();
} foreach (var task in tasks)
task.Wait();
} public void WriteToDB()
{
try
{
using (SQLiteConnection myconnection = new SQLiteConnection(@"Data Source=c:\123.db"))
{
myconnection.Open();
using (SQLiteTransaction mytransaction = myconnection.BeginTransaction())
{
using (SQLiteCommand mycommand = new SQLiteCommand(myconnection))
{
Guid id = Guid.NewGuid(); mycommand.CommandText = "INSERT INTO Categories(ID, Name) VALUES ('" + id.ToString() + "', '111')";
mycommand.ExecuteNonQuery(); mycommand.CommandText = "UPDATE Categories SET Name='222' WHERE ID='" + id.ToString() + "'";
mycommand.ExecuteNonQuery(); mycommand.CommandText = "DELETE FROM Categories WHERE ID='" + id.ToString() + "'";
mycommand.ExecuteNonQuery();
}
mytransaction.Commit();
}
}
}
catch (SQLiteException ex)
{
if (ex.ReturnCode == SQLiteErrorCode.Busy)
Console.WriteLine("Database is locked by another process!");
}
}
}
}
SQLite multiple threads的更多相关文章
- Multiple Threads reading from the same file(转载)
问 I have a xml file that needs to be read from many many times. I am trying to use the Parallel.ForE ...
- Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类
Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...
- caffe网络在多线程中无法使用GPU的解决方案 | cpp caffe net run in multiple threads
本文首发于个人博客https://kezunlin.me/post/8d877e63/,欢迎阅读! cpp caffe net run in multiple threads Guide set_mo ...
- bsxfun.h multiple threads backup
https://code.google.com/p/deep-learning-faces/source/browse/trunk/cuda_ut/include/bsxfun.h?r=7&s ...
- Hashtable insert failed. Load factor too high. The most common cause is multiple threads writing to the Hashtable simultaneously
暂时也没准确定位到问题 https://support.microsoft.com/zh-cn/help/2803754/hotfix-rollup-2803754-is-available-for- ...
- 临界区代码 critical section Locks and critical sections in multiple threads
临界区 在同步的程序设计中,临界区段(Critical section)指的是一个访问共享资源(例如:共享设备或是共享存储器)的程序片段,而这些共享资源有无法同时被多个线程访问的特性. 当有线程进入临 ...
- PatentTips - Controlling TSC offsets for multiple cores and threads
BACKGROUND Many processors include a time stamp count (TSC) counter which is typically implemented a ...
- 【腾讯Bugly干货分享】微信iOS SQLite源码优化实践
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57b58022433221be01499480 作者:张三华 前言 随着微信iO ...
- SQLite源程序分析之sqlite3.c
/****************************************************************************** ** This file is an a ...
随机推荐
- lbs@node(lbs asp blog 移植到 nodejs)
lbs@node 2018年的4月26日,我在自己的idea清单中,加上了一条"基于 nodejs 移植 lbs 博客系统". 一.lbs 是什么东东? 它是一款比较小众的博客程序 ...
- layui form表单自定义sm格式
1. 新建以下sm样式,保存为layform_sm.css文件名,然后导入到layui.css的后面. .layui-input-sm,.layui-select-sm,.layui-textarea ...
- 【QT】二进制读取图像文件测试
QDataStream in(&file); int n; in >> n ; file.close(); qDebug() << n<<"en& ...
- 【学习】C++异常处理,exception
try.catch try { ... }中的语句按顺序执行,突然遇到错误了,则捕获,catch. try中语句执行到最后可以加一条语句,打印出,程序成功运行. catch(const .....e) ...
- Library弱依赖打包
为减少强依赖,运行时动态监测依赖是否存在. 例如:内置的 HTTP client 可以是 OkHttpClient 或者是 HttpURLConnection.前者拥有更高的性能,但需要引入 OkHt ...
- vue里使用create、mounted调用方法的正确姿势
参考:https://blog.csdn.net/Honnyee/article/details/82421844
- kill、killall、pkill杀手三人组
1.1 kill.killall.pkill杀手三人组 1.利用kill 进程号 方式杀掉rsync进程 [root@backup ~]# ps -ef |grep rsync root 3500 1 ...
- JS实现表格使用上下左右键聚集
//调用:new tabTableInput("tblGrid","text"); var tabTableInput = function (tableId, ...
- python全栈开发 * 36知识点汇总 * 180721
36 操作系统的发展史 进程一.手工操作--穿孔卡片 1.内容: 程序员将对应于程序和数据的已穿孔的纸带(或卡片)装入输入机,然后启动输入机把程序和数据输入计算机内存,接着通过控制 台开关启动程序针对 ...
- CentOS使用systemctl daemon-reload报错Error getting authority: Error initializing authority: Error calling StartServiceByName for org.freedesktop.PolicyKit1: Timeout was reached (g-io-error-quark, 24)解决办法
CentOS修改了系统启动文件后需要重载报错 systemctl daemon-reload Error getting authority: Error initializing authority ...