LOTUSSCRIPT 言語


同期化の仕組み
同期化では 1 つの CPU を複数タスク (スレッド) が共有しますが、この共有は、スレッドの切り替えに要する時間を最小化できるような方法で行われます。スレッド対応のサーバー上ではエージェントは交互にタスクを実行しますが、時間が節約されるので、あたかもタスクが同時に発生しているかのように見えます。

次のサンプルエージェント Comm1 では、類似したエージェント Comm2 と同時に動作します。この例では、エージェントを同時に実行する場合にどのようにコードにロックをかけるかを説明しています。

これらのエージェントは同時に起動し、「Update」という名のロックを掛けます。その後、以下の処理が行われます。


このプロセスは、For ループが続く限り繰り返されます。この例の場合では 5 回繰り返されます。

'Comm1:

Option Public

' Remove the following line if you do not have a
' resource library defined.
Use "ThreadsLib"

Sub Initialize
  Dim lockName As String
  Dim lockID As Integer, refcnt As Integer
  Dim gotLock As Variant,  releaseLock As Variant, _
   deleteLock As Variant

On Error Goto syn_error

' Provide some unique name here to distinguish the agents.
  ID = "Comm1  tuvwx:5706 "
  Msgbox  ID & "Started"

lockName = "Update"
  On Error Goto syn_error

' Create the lock
  lockID = Createlock(uName)
  If (lockID <> -1) Then
     Msgbox ID & "Created lock:" & lockID
  End If

' Put agent to sleep for a second.
  ' This gives the second agent time to start.
  Sleep 1

For x = 1 To 5

' Attempt to get the lock and report the outcome
     ' as well as the reference count
     gotLock = CodeLock(lockID)
     If (gotLock) Then  
        Msgbox ID & " Got lock:" & lockID & " - at:" & _
          Now()
        refcnt = Codelockcheck(lockID)
        Msgbox ID & "      Reference count is " & refcnt

' Do some meaningful work here, or just sleep
        Sleep 1
     Else
        Msgbox ID & "Failed to get lock"
     End If

' Release the lock so the other agent can get it.
     releaseLock = Codeunlock(lockID)
     If (releaseLock) Then
        Msgbox ID & " Releasing lock:" & lockID & _
          " - at:" & Now()

' Sleep here allows the other agents to obtain
        ' the lock before this agent has a chance to.
        Sleep 1
     Else
        Msgbox ID & "Failed to release lock"
     End If

Next

' When we are finished, destroy this agent's reference
  ' to the lock
  deleteLock = Destroylock(lockID)
  If (deleteLock ) Then
     Msgbox ID & "Destroyed lock " & lockID
  Else
     Msgbox ID & "Failed to destroy lock"
  End If

Msgbox  ID & "Done "
  Exit Sub

syn_error:
  errormsg  = "  * * Error:" & Err & " - " &  Error() & _
   " in " & ID & " at " & Erl()
  Msgbox errormsg
  Resume Next

End Sub

次は出力結果の例です。Comm1 (ID tuvwx:5706) と Comm2 (ID uvwxy:5742) は Lotus Domino Web サーバーのエージェントとして同時に実行され、次のように出力します。

メモ このスクリプトを実際に実行しても、出力結果は必ずしもこの例と同じにはなりません。これは非同時性のシステムロックの性能により、何らかの予期できないイベントが発生する可能性があるためです。

Addin:Agent message box:Comm1 tuvwx:5706 Started
Addin:Agent message box:Comm1 tuvwx:5706 Created lock:0
Addin:Agent message box:Comm2 uvwxy:5742 Started
Addin:Agent message box:Comm2 uvwxy:5742 Created lock:0
Addin:Agent message box:Comm1 tuvwx:5706 Got lock:0 - at:2/10/99 01:57:06 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Reference count is 1
Addin:Agent message box:Comm2 uvwxy:5742 Got lock:0 - at:2/10/99 01:57:07 PM
Addin:Agent message box:Comm2 uvwxy:5742 Reference count is 1
Addin:Agent message box:Comm1 tuvwx:5706 Releasing update_lock:0 - at:2/10/99 01:57:07 PM
Addin:Agent message box:Comm2 uvwxy:5742 Releasing update_lock:0 - at:2/10/99 01:57:08 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Got lock:0 - at:2/10/99 01:57:08 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Reference count is 1
Addin:Agent message box:Comm2 uvwxy:5742 Got lock:0 - at:2/10/99 01:57:09 午前 PM
Addin:Agent message box:Comm2 uvwxy:5742 Reference count is 1
Addin:Agent message box:Comm1 tuvwx:5706 Releasing lock:0 - at:2/10/99 01:57:09 午前 PM
Addin:Agent message box:Comm2 uvwxy:5742 Releasing lock:0 - at:2/10/99 01:57:10 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Got lock:0 - at:2/10/99 01:57:10 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Reference count is 1
Addin:Agent message box:Comm2 uvwxy:5742 Got lock:0 - at:2/10/99 01:57:12 午前 PM
Addin:Agent message box:Comm2 uvwxy:5742 Reference count is 1
Addin:Agent message box:Comm1 tuvwx:5706 Releasing lock:0 - at:2/10/99 01:57:12 午前 PM
Addin:Agent message box:Comm2 uvwxy:5742 Releasing lock:0 - at:2/10/99 01:57:13 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Got lock:0 - at:2/10/99 01:57:13 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Reference count is 1
Addin:Agent message box:Comm2 uvwxy:5742 Got lock:0 - at:2/10/99 01:57:14 午前 PM
Addin:Agent message box:Comm2 uvwxy:5742 Reference count is 1
Addin:Agent message box:Comm1 tuvwx:5706 Releasing lock:0 - at:2/10/99 01:57:14 午前 PM
Addin:Agent message box:Comm2 uvwxy:5742 Releasing lock:0 - at:2/10/99 01:57:15 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Got lock:0 - at:2/10/99 01:57:15 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Reference count is 1
Addin:Agent message box:Comm2 uvwxy:5742 Got lock:0 - at:2/10/99 01:57:16 午前 PM
Addin:Agent message box:Comm2 uvwxy:5742 Reference count is 1
Addin:Agent message box:Comm1 tuvwx:5706 Releasing lock:0 - at:2/10/99 01:57:16 午前 PM
Addin:Agent message box:Comm2 uvwxy:5742 Releasing lock:0 - at:2/10/99 01:57:18 午前 PM
Addin:Agent message box:Comm1 tuvwx:5706 Destroyed lock 0
Addin:Agent message box:Comm1 tuvwx:5706 Done
Addin:Agent message box:Comm2 uvwxy:5742 Destroyed lock 0
Addin:Agent message box:Comm2 uvwxy:5742 Done

これらの関数は、1 つのプロセス内の LotusScript 協調エージェントのインスタンス間の通信だけに使用されます。これらは、非同期 Web エージェント用に特に設計されているものです。

サポートされるプラットフォームは、Win32、OS/2、UNIX (Solaris、HP-UX、AIX)、および NT Alpha です。

関連項目