我想我有一个非常糟糕的概念问题.为什么我简单地用valgrind获得了很多竞争条件错误.首先我认为这可能是一个错误,我在论坛中看到linux的更新滚动版本将解决这个问题...所以现在我已经打开了tubleweed,100%更新.以下代码muss有一些非常错误:
#include <iostream>
#include <thread>
using namespace std;
class FOO
{
public:
void do_something ()
{
cout<<"cout somethin\n";
}
};
int main ()
{
FOO foo;
std::thread t (&FOO::do_something,&foo);
t.join();
}
Run Code Online (Sandbox Code Playgroud)
当我做的时候
valgrind --tool=drd ./oncordia
Run Code Online (Sandbox Code Playgroud)
我必须尝试5次或更多,我得到以下错误:
==6218== drd, a thread error detector
==6218== Copyright (C) 2006-2010, and GNU GPL'd, by Bart Van Assche.
==6218== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==6218== Command: ./oncordia
==6218==
cout somethin
==6218== Conflicting store by thread 1 at 0x05b5d050 size 8 …
Run Code Online (Sandbox Code Playgroud) 我在 GUI 线程中将此函数称为:
let updateImageLoop (pprocess : PlotProcess) (target : IUpdatableImageView<'T>) =
async {
while target.Continue do
let context = System.Threading.SynchronizationContext.Current
do! Async.SwitchToThreadPool()
System.Threading.Thread.Sleep(10000)
do! Async.SwitchToContext(context)
let image = target.CreateImage()
match image with
| Some userImage -> do! target.UpdateImageView userImage
| None -> ()
} |> Async.StartImmediate
Run Code Online (Sandbox Code Playgroud)
问题出现在执行target.UpdateImageView方法时,产生异常:
调用线程必须是 STA,因为许多 UI 组件都需要它。
我知道,但这就是我所做的
do! Async.SwitchToContext(context)
Run Code Online (Sandbox Code Playgroud)
消除 SwitchToContext 和 SwitchToThreadPool 函数可以消除异常,但 GUI 只是冻结。这是有道理的,但为什么我不能在线程之间切换?
产生问题的函数是UpdateImageView。我在异步和不异步的情况下测试了它。
member this.UpdateImageView etoimage =
async {
let imageview = new Eto.Forms.ImageView()
imageview.Image <- etoimage
this.Content …
Run Code Online (Sandbox Code Playgroud)