アットウィキロゴ

Example17.6

「Example17.6」の編集履歴(バックアップ)一覧に戻る

Example17.6 - (2011/02/24 (木) 09:14:18) のソース

#co(){
17.6 Readers/Writers

A more complex form of synchronization distinguishes between readers which access a common resource without modifying it and writers which can both access and modify it. To synchronize readers and writers we need to implement operations startRead, startWrite, endRead, endWrite, such that:

- there can be multiple concurrent readers,
- there can only be one writer at one time,
- pending write requests have priority over pending read requests, but don'tpreempt ongoing read operations.
}


#setmenu2(ex-r-menu)
** 17.6 リーダー/ライター

より複雑な同期の形態では、共通リソースを変更することなくアクセスする&bold(){リーダー}と、アクセスと変更の両方が可能な&bold(){ライター}を区別します。リーダーとライターを同期させるために startRead、startWrite、endRead、endWrite、その他を実装する必要があります。

- 多数の並行リーダーがある
- 同時にはただ1つのライターのみ可能
- ペンディング中の書き込み要求は、ペンディング中の読み込み要求よりも優先するが、実行中のリード操作をプリエンプトしない

#co(){
The following implementation of a readers/writers lock is based on the mai
lbox concept (see Section 17.10).
}

次のリーダー/ライター ロックの実装は&bold(){メールボックス}に基づいています (17.10節参照)。

  import scala.concurrent._
 
  class ReadersWriters {
    val m = new MailBox
    private case class Writers(n: Int), Readers(n: Int) { m send this }
    Writers(0); Readers(0)
    def startRead = m receive {
      case Writers(n) if n == 0 => m receive {
        case Readers(n) => Writers(0); Readers(n+1)
      }
    }
    def startWrite = m receive {
      case Writers(n) =>
        Writers(n+1)
        m receive { case Readers(n) if n == 0 => }
    }
    def endRead = m receive {
      case Readers(n) => Readers(n-1)
    }
    def endWrite = m receive {
      case Writers(n) => Writers(n-1); if (n == 0) Readers(0)
    }
  }

#center(){[[前ページ>Example17.5]] [[ 17 章>Chapter 17 Abstractions for Concurrency]] [[目次>ScalaByExample和訳]] [[次ページ>Example17.7]]}

----
#comment
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。