C# 为什么这个程序会出错?`从未同步的代码块调用对象同步方法`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1894905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is this program in error? `Object synchronization method was called from an unsynchronized block of code`
提问by
What is wrong with this code? i get a 'Object synchronization method was called from an unsynchronized block of code'. I found one result on google that said i may be releasing a mutex before locking but according to my output this is not the case. Here is the mutex code without the other code in between.
这段代码有什么问题?我得到一个'对象同步方法是从一个未同步的代码块调用的'。我在 google 上发现一个结果,说我可能会在锁定之前释放互斥锁,但根据我的输出,情况并非如此。这是互斥代码,中间没有其他代码。
-edit- sorry guys, wrong paste.
- 编辑 - 对不起,错误的粘贴。
My output
我的输出
1W
1W
2W
code
代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace sqliteTest
{
class Program
{
static volatile Mutex mut1 = new Mutex();
static volatile Mutex mut2 = new Mutex();
static void Main(string[] args)
{
mut1.WaitOne(); Console.WriteLine("1W");
Thread oThread = new Thread(new ThreadStart(fn2));
oThread.Start();
mut1.WaitOne(); Console.WriteLine("1W");
update(0);
}
static void fn2()
{
mut2.WaitOne(); Console.WriteLine("2W");
mut1.ReleaseMutex(); Console.WriteLine("1R");
mut2.WaitOne(); Console.WriteLine("2W");
update(1);
mut1.ReleaseMutex(); Console.WriteLine("1R");
}
static void update(int t)
{
mut2.ReleaseMutex(); Console.WriteLine("2R");
if (t == 0)
{
mut1.WaitOne();
Console.WriteLine("1W");
}
}
}
}
回答by Mark Byers
Your code seems to have a number of problems. The main thread starts a new thread, then calls update. In update it tries to unlock mutex2, but it hasn't locked mutex2 yet, so this fails with an exception.
您的代码似乎有很多问题。主线程启动一个新线程,然后调用更新。在更新中,它尝试解锁 mutex2,但它还没有锁定 mutex2,因此这会失败并出现异常。
Even if this error is fixed, the other thread is equally doomed to failure. It will try to release mutex1 before it has locked it.
即使修复了这个错误,另一个线程也同样注定要失败。它将尝试在锁定 mutex1 之前释放它。
What are you trying to do here? Are you confusing Mutex with AutoResetEvent?
你想在这里做什么?您是否将 Mutex 与AutoResetEvent混淆?
Also I'm guessing that these two lines are a copy/paste error because they appear twice:
另外我猜这两行是复制/粘贴错误,因为它们出现了两次:
mut2.WaitOne(); Console.WriteLine("2W");
mut1.ReleaseMutex(); Console.WriteLine("1R");
回答by Hans Passant
It is not a great error message, Windows produces it. What it really means is that you are calling ReleaseMutex on a mutex that you don't own. You'll get past the first exception with
这不是一个很好的错误消息,Windows 会生成它。它的真正含义是您在不拥有的互斥锁上调用 ReleaseMutex。你会通过第一个例外
static volatile Mutex mut2 = new Mutex(true);
But then it will die inside the thread when it calls ReleaseMutex on mut1, which it doesn't own. Not sure what you're trying to do, the code doesn't make much sense to me.
但是当它在它不拥有的 mut1 上调用 ReleaseMutex 时,它将在线程内死亡。不确定您要做什么,代码对我来说没有多大意义。
回答by Elmü
Yes the others are right: This code does not make any sense.
是的,其他人是对的:这段代码没有任何意义。
But as this page has a high ranking in Google (although it is not helpfull) I redirect all the searchers to this great site about Thread synchronization:
但是由于这个页面在谷歌中排名很高(虽然它没有帮助),我将所有搜索者重定向到这个关于线程同步的好网站:
http://www.albahari.com/threading/part2.aspx
http://www.albahari.com/threading/part2.aspx
Elmü
埃尔穆
回答by ouflak
Just wanted to post another odd scenario I ran into as it might help others. I was getting this exception when opening up a project in Visual Studio 2010 C# express. Now my project does contain some threading, but I wouldn't expect it to crash Visual Studio on loading the code, which is exactly what was happening.
只是想发布另一个我遇到的奇怪场景,因为它可能会帮助其他人。在 Visual Studio 2010 C# express 中打开项目时遇到此异常。现在我的项目确实包含一些线程,但我不希望它在加载代码时使 Visual Studio 崩溃,这正是正在发生的事情。
A note about my code: I am using MethodInvoker and BeginInvoke (after checking if InvokeRequired) called on a STA thread ([STAThreadAttribute]). Again, this code never gets a chance to even run. Visual Studio Express would crash just loading up the project. Also, this all started happening after I got a strange FileNotFoundException that was likewise crashing VS when it claimed it couldn't find a reference. I fixed that by eventually completely deleting the reference and all of its usage from the code, building, closing VS, opening it up again and opening the solution up, only to then begin hitting this mysterious error. No Mutexes or Monitors in this project or associated with that previously removed reference (a custom library that just draws some graphs).
关于我的代码的说明:我正在使用在 STA 线程 ([STAThreadAttribute]) 上调用的 MethodInvoker 和 BeginInvoke(在检查 InvokeRequired 之后)。同样,这段代码永远没有机会运行。Visual Studio Express 会在加载项目时崩溃。此外,这一切都是在我收到一个奇怪的 FileNotFoundException 之后开始发生的,当它声称找不到引用时,它同样使 VS 崩溃。我通过最终从代码中完全删除引用及其所有用法来解决这个问题,构建,关闭 VS,再次打开它并打开解决方案,然后才开始遇到这个神秘的错误。此项目中或与先前删除的引用(仅绘制一些图形的自定义库)相关联的互斥锁或监视器。
After considerable research (googling) where I found a load of interesting information that had nothing to do with VS crashing, I finally decided to just try and open up the solution in Visual Studio 2010 Professional to see if it would crash that as well. It didn't. I made a couple of token changes to the code and project settings, built and saved everything, shut it down, and now I can open the solution again in Visual Studio Express without any error! Very strange. Don't know if this will help anybody as this seems a bit on the fringe of possibilities for this exception. I suspect there was an issue with my registry of all things as I've run into a few curious situations in the past with Visual Studio and my registry not getting along, all of those affecting how my projects load up, even forcing me to restore my registry, run a registry cleaner, and reboot to get normal behavior back again.
经过大量研究(谷歌搜索),我发现了大量与 VS 崩溃无关的有趣信息,我最终决定尝试在 Visual Studio 2010 Professional 中打开解决方案,看看它是否也会崩溃。它没有。我对代码和项目设置进行了一些标记更改,构建并保存了所有内容,然后将其关闭,现在我可以在 Visual Studio Express 中再次打开解决方案而不会出现任何错误!很奇怪。不知道这是否会帮助任何人,因为这似乎有点超出此例外的可能性。我怀疑我的所有东西的注册表都存在问题,因为我过去在 Visual Studio 中遇到过一些奇怪的情况,而且我的注册表不合时宜,所有这些都会影响我的项目加载方式,甚至迫使我恢复我的注册表,