假设您有一个具有以下定义的表:
CREATE TABLE public.positions
(
id serial,
latitude numeric(18,12),
longitude numeric(18,12),
updated_at timestamp without time zone
)
Run Code Online (Sandbox Code Playgroud)
你在这样的表中有 50,000 行。现在出于测试目的,您将运行如下更新:
update positions
set updated_at = now()
where latitude between 234.12 and 235.00;
Run Code Online (Sandbox Code Playgroud)
该语句将更新 50,000 行中的 1,000 行(在此特定数据集中)
如果您在 30 个不同的线程中运行这样的查询,MySQL innodb 将成功,而 postgres 将因大量死锁而失败。
为什么?
我在我的centos计算机上测试c ++ 11互斥.我尝试双重锁定此互斥锁以使其死锁.但是在我运行它之后,一切都很好并且没有发生死锁.
#include <thread>
#include <mutex>
#include <iostream>
std::mutex m;
int main()
{
m.lock();
m.lock();
std::cout<<"i am ok"<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器是g ++ 4.8.5 in centos 3.10.0-327.36.3.el7.x86_64:
[zzhao010@localhost shareLibPlay]$ ./3.out
i am ok
Run Code Online (Sandbox Code Playgroud) 我有一个(LRU)缓存对象,我遇到了死锁......这怎么可能?
type cache struct {
mutex *sync.Mutex
...
}
func (this *cache) Init() { // guaranteed to be called once, in main()
this.mutex = &sync.Mutex{}
}
func (this *cache) f1() {
// Pattern for accessing mute, at the top of any function of 'cache' where needed.
this.mutex.Lock()
defer this.mutex.Unlock()
...
}
func (this *cache) f2() {
this.mutex.Lock()
defer this.mutex.Unlock()
...
}
Run Code Online (Sandbox Code Playgroud)
在mutex出现的每个函数中,仅使用此模式访问它.然而......我陷入僵局.怎么可能呢?
注意:此代码已在生产服务器上运行了10个月,这是我第一次获得该代码.
编辑:所以f1()可以调用(间接)f2()来根据答案获得死锁.是的,但在我的代码中,这不会发生,所以我真的很想知道
我是异步等待的新手.我想我理解了控制台应用程序的例子.将相同的代码传输到WPF时,会出现死锁,我不确切知道原因.
// All as expected
// Output:
// LongOperationAsync Start
// Before task.Wait();
// LongOperationAsync End
// After task.Wait();
// Result: 4711
class Program {
public static void Main() {
Task<int> task = LongOperationAsync();
//Console.ReadKey();
Console.WriteLine("Before task.Wait();");
task.Wait();
Console.WriteLine("After task.Wait();");
var result = task.Result;
Console.WriteLine("Result: {0}", result);
Console.ReadKey();
}
static async Task<int> LongOperationAsync() {
Console.WriteLine("LongOperationAsync Start");
await Task.Delay(1000);
Console.WriteLine("LongOperationAsync End");
return 4711;
}
}
Run Code Online (Sandbox Code Playgroud)
这是阻止WPF代码:
// WPF handler Output:
// LongOperationAsync Start
// Before task.Wait(); => Blocking
private void button2_Click(object …Run Code Online (Sandbox Code Playgroud) 首先,对于另一个“为什么我的异步操作挂起”问题感到抱歉,但我相信这个问题已经足够不同了。
调查了几十个类似的问题,异步操作死锁的问题要么是把自己锁在外面(.Result),使用有限的资源,要么错误地使用库组件(网络请求似乎很流行)。在以下示例中,我找不到上面的任何内容:
private async Task ExecuteAsync(Task<int> task)
{
// entering on current thread, that is the main UI thread
await task // execute "task" asynchronnously (on a different thread)
.ConfigureAwait(false); // when done, no need to return to main thread
MessageBox.Show("success"); // succes indicator
}
public MainWindow() //wpf window ctor
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var task = new Task<int>(() => 42); // make an Action wrapping sychronnous …Run Code Online (Sandbox Code Playgroud) 当我学习shared_mutexC++17时,我发现一个奇怪的问题。如果我在一个线程中调用两次,然后在另一个线程中shared_lock调用,那么我的程序将冻结。unique_lock像这样:
std::mutex mutex;
std::shared_mutex s_mutex;
int i = 0;
void func() {
auto lock = std::shared_lock(s_mutex);
auto lock1 = std::shared_lock(s_mutex);
++i;
}
void func2() {
auto lock = std::unique_lock(s_mutex);
++i;
}
int main() {
auto t1 = std::thread([](){
auto i = 10000;
while(i--) {
func();
}
});
auto t2 = std::thread([](){
auto i = 10000;
while(i--) {
func2();
}
});
t2.join();
t1.join();
std::cout << i << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个问题好像只出现在windows上,我在Arch linux上尝试过,效果很好。
我在用着g++.exe …
有人可以用C++中的两个线程示例给我一个简单的死锁
如何在第8或第16行发生死锁?
1. public class DeadlockRisk {
2. private static class Resource {
3. public int value;
4. }
5. private Resource resourceA = new Resource();
6. private Resource resourceB = new Resource();
7. public int read() {
8. synchronized(resourceA) {
9. synchronized(resourceB) {
10. return resourceB.value + resourceA.value;
11. }
12. }
13. }
14
15. public void write(int a, int b) {
16. synchronized(resourceB) {
17. synchronized(resourceA) {
18. resourceA.value = a;
19. resourceB.value = b;
20. }
21. …Run Code Online (Sandbox Code Playgroud) 这是我的最低代表案例:
public Form1()
{
Task.Delay(100).Wait(); // Works just fine
this.Await().Wait(); // Blocks indefinitely
}
private async Task Await()
{
await Task.Delay(100);
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?为什么这两个表现不一样?我该怎么做才能使后一个工作?
我的实际案例不那么简单,我不能"只使用第一个选项".
我正在使用Delphi XE2,我的应用程序用于通知twitter/rss中的新记录.在我的应用程序中,我使用2个线程每秒从twitter和rss中获取一些数据.
这是代码:
类型部分:
TWarframeEvent=record
GUID: String;
EventType: Byte; // 0 = unknown; 1 = alert; 2 = invasion; 3 = infestation
Planet: String;
Mission: String;
EventDate: TDateTime;
Time: Integer;
RewardCredits: LongWord;
RewardOther: String;
RewardOtherAmount: Integer;
Notified: Boolean;
ItemIndex: Integer;
Hidden: Boolean;
end;
TWarframeNotifyEvent=record
NotifyTimeLeft: LongWord;
ID: Integer;
FlashOnTaskbar: Boolean;
PlaySound: Boolean;
Volume: Integer;
TrayPopupBalloon: Boolean;
end;
TWarframeEventList=record
WarframeEvent: Array of TWarframeEvent;
WarframeEventCount: Integer;
NotifyEvent: TWarframeNotifyEvent;
end;
TUpdateFromTwitterThread=class(TThread)
TwitterURL: String;
Procedure Execute; override;
end;
TUpdateFromRSSThread=class(TThread)
RSS_URL: String;
Procedure Execute; override; …Run Code Online (Sandbox Code Playgroud) 假设我有这两种结构:
type A struct {
Mutex sync.Mutex
i int
}
type B struct {
A
sync.Mutex
}
Run Code Online (Sandbox Code Playgroud)
现在,当我试图锁定B然后A我遇到了僵局:
var b B
b.Lock()
b.Mutex.Lock()
b.Mutex.Unlock()
b.Unlock()
Run Code Online (Sandbox Code Playgroud)
我想通了,这是与结构的互斥体的名字相关A,例如,有是,如果我将其命名为无僵局Mutexx,而不是Mutex.但我不知道为什么这很重要.有人可以解释一下这种行为吗?
根据这里的定义,死锁与资源争用有关。
在操作系统中,当一个进程或线程进入等待状态时会发生死锁,因为请求的系统资源被另一个等待进程持有,而另一个等待进程又在等待另一个等待进程持有的另一个资源。如果一个进程不能无限期地改变它的状态,因为它请求的资源正在被另一个等待的进程使用,那么系统就被称为死锁。
在下面的代码中:
package main
import "fmt"
func main() {
c := make(chan string)
c <- "John"
fmt.Println("main() stopped")
}
Run Code Online (Sandbox Code Playgroud)
main() go-routine 阻塞,直到任何其他 go-routine(没有这样的)从该通道读取相同的数据。
但输出显示:
$ bin/cs61a
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()
/home/user/../myhub/cs61a/Main.go:8 +0x54
$
Run Code Online (Sandbox Code Playgroud)
编辑:
重点是:“主 goroutine 被阻塞,因此所有 goroutine 都被阻塞,因此这是一个死锁。” 在下面的代码中,非主协程也在通道上被阻塞,不是所有的协程都应该被阻塞吗?
package main
import (
"fmt"
"time"
)
func makeRandom(randoms chan int) {
var ch chan int
fmt.Printf("print 1\n")
<-ch
fmt.Printf("print 2\n")
}
func main() {
randoms := make(chan …Run Code Online (Sandbox Code Playgroud) deadlock ×12
async-await ×3
c# ×3
go ×3
c++ ×2
mutex ×2
.net ×1
asynchronous ×1
c++11 ×1
delphi ×1
embedding ×1
goroutine ×1
java ×1
mysql ×1
postgresql ×1
pthreads ×1
reusability ×1
struct ×1
visual-c++ ×1
wpf ×1