2017-12-28 14 views
-1

私の状況はこれです:が一度にただ一つのスレッドによって実行される

  • 私はすでにマルチスレッドで実行されているコードの一部へのアクセス権を持っているので、私は」スレッドを作成せず、スレッドが作成されているコード部分にアクセスできない
  • かなり複雑な(複数の関数が呼び出され、DBアクセスなどが含まれている)コードの部分が一度に1つのスレッドによって排他的に実行されます

それは可能ですか?もしそうなら、それを行う最善の方法は何ですか?

:私は小さな調査の努力をしました、ここで尋ねると終わりがこれを読む前に

// This method is executed by multiple threads at the same time 
protected override void OnProcessItem() 
{ 
    MyObject myObject = new MyObject(); 

    // Begin of the code that should be executed only once at a time 
    // by each thread 

    bool result = myObject.DoComplexStuff(); 

    if(result == true) 
    { 
     this.Logger.LogEvent("Write something to log"); 
     // ... Do more stuff 
    } 
    else 
    { 
     this.Logger.LogEvent("Write something else to log"); 
     // ... Do more stuff 
    } 

    // ... Do more stuff 

    // End of the code that should be executed only once at a time 
    // by each thread 
} 

いくつかの背景

:ここ

は私がacomplishしようとしているもののいくつかの例示的なコードです

https://docs.microsoft.com/en-us/dotnet/standard/threading/overview-of-synchronization-primitives

しかし、私は最良のアプローチが何であるかわからないmiのシナリオでそれを実装する方法。

さらに、私はあなたが推測しているように、マルチスレッドの専門家ではありません。

+3

を働いているかlock上のいくつかのコメントを追加しましたあなたは 'lock'をお探しですか? 'lock(syncObject){/ *一度に1つのスレッド* /}'?あなたのケースでは、 'lock(this.Logger){...} 'となる可能性があります。 –

+0

重複したマーキングについては、他の記事の回答は完全にここに当てはまりますが、より一般的な方法でこれを探しているなら、それを見つけてください。 –

答えて

1

はい、これにはlockを使用できます。クラス内にプライベート同期オブジェクトを作成し、このオブジェクトのロックを取得し、一度に1つのスレッドだけがlockブロック内のコードを実行できるようにします。

class Foo 
{ 
    private readonly object syncObj = new object(); 

    public void Blah() 
    { 
      lock (syncObj) 
      { 
       //only one thread at a time 
       //will execute this code 
      } 
    } 
} 
+0

私は実際にオブジェクトのロックについて読んでいますが、同期のためのダミーオブジェクトを作成することは決してありませんでした。ロックアプローチを使うためには、ただ一つのオブジェクトをロックする必要がありました。どうもありがとう。 –

+1

@ManuelNavarroよろしくお願いします! 1つの小さなコメント:上記のコードは、Foo *の各インスタンスについて、ロック内のコードが一度に1つのスレッドによって実行されることを保証します。必要なのは、コードが 'Foo'のどのインスタンスであっても1つのスレッドだけで実行できるということです。それで' syncObj'を静的にする必要があります。 – InBetween

2

最も簡単な方法は、例えば、lockを置くことである。:

// static: conservative policy: no two threads in the entire application 
// can enter locked block at the same time. 
// You may want to be more lenient and lock, say on this.Logger i.e. 
// no two theards addressing the same Logger... 
private static object syncObject = new object(); 

... 

protected override void OnProcessItem() { 
    MyObject myObject = new MyObject(); 

    // Begin of the code that should be executed only once at a time 
    lock (syncObject) { 
    bool result = myObject.DoComplexStuff(); 

    if (result) { 
     this.Logger.LogEvent("Write something to log"); 

     // ... Do more stuff 
    } 
    else { 
     this.Logger.LogEvent("Write something else to log"); 

     // ... Do more stuff 
    } 

    // ... Do more stuff 
    } 
    // End of the code that should be executed only once at a time by each thread 
} 
0

私は

private object _OnProcessItemLockObject = new object(); 
// This method is executed by multiple threads at the same time 
protected override void OnProcessItem() 
{ 
    MyObject myObject = new MyObject(); 

    // Begin of the code that should be executed only once at a time 
    // by each thread 

    bool result = false; 
    lock(_OnProcessItemLockObject) 
    { 
     //mat: the part inside the lock will only be executed by one thread. All other threads will wait till the lock-object is unlocked. 
     result = myObject.DoComplexStuff(); 
    } 

    //mat: the following code will get executed parallel 
    if(result == true) 
    { 
     this.Logger.LogEvent("Write something to log"); 
     // ... Do more stuff 
    } 
    else 
    { 
     this.Logger.LogEvent("Write something else to log"); 
     // ... Do more stuff 
    } 

    // ... Do more stuff 

    // End of the code that should be executed only once at a time 
    // by each thread 
} 
関連する問題