Semaphore.Release メソッド ()
アセンブリ: System (system.dll 内)

Dim instance As Semaphore Dim returnValue As Integer returnValue = instance.Release
Release メソッドが呼び出される前のセマフォのカウント。


通常、スレッドは WaitOne メソッドを使用してセマフォに入り、このメソッド オーバーロードを使用して出ます。
Release メソッドから SemaphoreFullException がスローされても、呼び出し側のスレッドに問題があるとは限りません。別のスレッドのプログラミング エラーが原因で、そのスレッドがセマフォに入った回数よりも多い回数だけセマフォから出たという可能性も考えられます。
現在の Semaphore オブジェクトが名前付きシステム セマフォを表している場合、ユーザーに SemaphoreRights.Modify 権限があり、セマフォが SemaphoreRights.Modify 権限で開かれている必要があります。

カウントの最大値が 3 で初期カウントが 0 のセマフォを作成するコード例を次に示します。この例では 5 つのスレッドが開始され、ブロックされてセマフォを待機します。メイン スレッドが Release(Int32) メソッド オーバーロードを使用してセマフォのカウントを最大値まで増加させ、3 つのスレッドがセマフォに入ることができるようにします。各スレッドは、動作をシミュレートするために System.Threading.Thread.Sleep メソッドを使用して 1 秒間待機し、その後 Release メソッド オーバーロードを呼び出してセマフォを解放します。
セマフォが解放されるたびに、前のセマフォのカウントが表示されます。コンソール メッセージは、セマフォの使用状況を追跡します。出力を読み取りやすくするために、シミュレートされた動作間隔がスレッドごとに若干増加します。
Imports System Imports System.Threading Public Class Example ' A semaphore that simulates a limited resource pool. ' Private Shared _pool As Semaphore ' A padding interval to make the output more orderly. Private Shared _padding As Integer <MTAThread> _ Public Shared Sub Main() ' Create a semaphore that can satisfy up to three ' concurrent requests. Use an initial count of zero, ' so that the entire semaphore count is initially ' owned by the main program thread. ' _pool = New Semaphore(0, 3) ' Create and start five numbered threads. ' For i As Integer = 1 To 5 Dim t As New Thread(New ParameterizedThreadStart(AddressOf Worker)) 'Dim t As New Thread(AddressOf Worker) ' Start the thread, passing the number. ' t.Start(i) Next i ' Wait for half a second, to allow all the ' threads to start and to block on the semaphore. ' Thread.Sleep(500) ' The main thread starts out holding the entire ' semaphore count. Calling Release(3) brings the ' semaphore count back to its maximum value, and ' allows the waiting threads to enter the semaphore, ' up to three at a time. ' Console.WriteLine("Main thread calls Release(3).") _pool.Release(3) Console.WriteLine("Main thread exits.") End Sub Private Shared Sub Worker(ByVal num As Object) ' Each worker thread begins by requesting the ' semaphore. Console.WriteLine("Thread {0} begins " _ & "and waits for the semaphore.", num) _pool.WaitOne() ' A padding interval to make the output more orderly. Dim padding As Integer = Interlocked.Add(_padding, 100) Console.WriteLine("Thread {0} enters the semaphore.", num) ' The thread's "work" consists of sleeping for ' about a second. Each thread "works" a little ' longer, just to make the output more orderly. ' Thread.Sleep(1000 + padding) Console.WriteLine("Thread {0} releases the semaphore.", num) Console.WriteLine("Thread {0} previous semaphore count: {1}", _ num, _ _pool.Release()) End Sub End Class
using System; using System.Threading; public class Example { // A semaphore that simulates a limited resource pool. // private static Semaphore _pool; // A padding interval to make the output more orderly. private static int _padding; public static void Main() { // Create a semaphore that can satisfy up to three // concurrent requests. Use an initial count of zero, // so that the entire semaphore count is initially // owned by the main program thread. // _pool = new Semaphore(0, 3); // Create and start five numbered threads. // for(int i = 1; i <= 5; i++) { Thread t = new Thread(new ParameterizedThreadStart(Worker)); // Start the thread, passing the number. // t.Start(i); } // Wait for half a second, to allow all the // threads to start and to block on the semaphore. // Thread.Sleep(500); // The main thread starts out holding the entire // semaphore count. Calling Release(3) brings the // semaphore count back to its maximum value, and // allows the waiting threads to enter the semaphore, // up to three at a time. // Console.WriteLine("Main thread calls Release(3)."); _pool.Release(3); Console.WriteLine("Main thread exits."); } private static void Worker(object num) { // Each worker thread begins by requesting the // semaphore. Console.WriteLine("Thread {0} begins " + "and waits for the semaphore.", num); _pool.WaitOne(); // A padding interval to make the output more orderly. int padding = Interlocked.Add(ref _padding, 100); Console.WriteLine("Thread {0} enters the semaphore.", num); // The thread's "work" consists of sleeping for // about a second. Each thread "works" a little // longer, just to make the output more orderly. // Thread.Sleep(1000 + padding); Console.WriteLine("Thread {0} releases the semaphore.", num); Console.WriteLine("Thread {0} previous semaphore count: {1}", num, _pool.Release()); } }
#using <System.dll> using namespace System; using namespace System::Threading; public ref class Example { private: // A semaphore that simulates a limited resource pool. // static Semaphore^ _pool; // A padding interval to make the output more orderly. static int _padding; public: static void Main() { // Create a semaphore that can satisfy up to three // concurrent requests. Use an initial count of zero, // so that the entire semaphore count is initially // owned by the main program thread. // _pool = gcnew Semaphore( 0,3 ); // Create and start five numbered threads. // for ( int i = 1; i <= 5; i++ ) { Thread^ t = gcnew Thread( gcnew ParameterizedThreadStart( Worker ) ); // Start the thread, passing the number. // t->Start( i ); } // Wait for half a second, to allow all the // threads to start and to block on the semaphore. // Thread::Sleep( 500 ); // The main thread starts out holding the entire // semaphore count. Calling Release(3) brings the // semaphore count back to its maximum value, and // allows the waiting threads to enter the semaphore, // up to three at a time. // Console::WriteLine( L"Main thread calls Release(3)." ); _pool->Release( 3 ); Console::WriteLine( L"Main thread exits." ); } private: static void Worker( Object^ num ) { // Each worker thread begins by requesting the // semaphore. Console::WriteLine( L"Thread {0} begins and waits for the semaphore.", num ); _pool->WaitOne(); // A padding interval to make the output more orderly. int padding = Interlocked::Add( _padding, 100 ); Console::WriteLine( L"Thread {0} enters the semaphore.", num ); // The thread's "work" consists of sleeping for // about a second. Each thread "works" a little // longer, just to make the output more orderly. // Thread::Sleep( 1000 + padding ); Console::WriteLine( L"Thread {0} releases the semaphore.", num ); Console::WriteLine( L"Thread {0} previous semaphore count: {1}", num, _pool->Release() ); } };
import System.*; import System.Threading.*; public class Example { // A semaphore that simulates a limited resource pool. // private static Semaphore _pool; // A padding interval to make the output more orderly. private static int _padding; public static void main(String[] args) { // Create a semaphore that can satisfy up to three // concurrent requests. Use an initial count of zero, // so that the entire semaphore count is initially // owned by the main program thread. // _pool = new Semaphore(0, 3); // Create and start five numbered threads. // for (int i = 1; i <= 5; i++) { System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(Worker)); // Start the thread, passing the number. // t.Start((Int32)i); } // Wait for half a second, to allow all the // threads to start and to block on the semaphore. // System.Threading.Thread.Sleep(500); // The main thread starts out holding the entire // semaphore count. Calling Release(3) brings the // semaphore count back to its maximum value, and // allows the waiting threads to enter the semaphore, // up to three at a time. // Console.WriteLine("main thread calls Release(3)."); _pool.Release(3); Console.WriteLine("main thread exits."); } //main private static void Worker(Object num) { // Each worker thread begins by requesting the // semaphore. Console.WriteLine("Thread {0} begins " + "and waits for the semaphore.", num); _pool.WaitOne(); // A padding interval to make the output more orderly. int padding = Interlocked.Add(_padding, 100); Console.WriteLine("Thread {0} enters the semaphore.", num); // The thread's "work" consists of sleeping for // about a second. Each thread "works" a little // longer, just to make the output more orderly. // System.Threading.Thread.Sleep(1000 + padding); Console.WriteLine("Thread {0} releases the semaphore.", num); Console.WriteLine("Thread {0} previous semaphore count: {1}", num, (Int32)_pool.Release()); } //Worker } //Example

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Semaphore.Release メソッド (Int32)
アセンブリ: System (system.dll 内)

Dim instance As Semaphore Dim releaseCount As Integer Dim returnValue As Integer returnValue = instance.Release(releaseCount)
戻り値
Release メソッドが呼び出される前のセマフォのカウント。


スレッドがセマフォに複数回入った場合、このメソッド オーバーロードにより、1 回の呼び出しでセマフォのカウント全体が復元されます。
Release メソッドから SemaphoreFullException がスローされても、呼び出し側のスレッドに問題があるとは限りません。別のスレッドのプログラミング エラーが原因で、そのスレッドがセマフォに入った回数よりも多い回数だけセマフォから出たという可能性も考えられます。
現在の Semaphore オブジェクトが名前付きシステム セマフォを表している場合、ユーザーに SemaphoreRights.Modify 権限があり、セマフォが SemaphoreRights.Modify 権限で開かれている必要があります。

カウントの最大値が 3 で初期カウントが 0 のセマフォを作成するコード例を次に示します。この例では 5 つのスレッドが開始され、ブロックされてセマフォを待機します。メイン スレッドが Release(Int32) メソッド オーバーロードを使用してセマフォのカウントを最大値まで増加させ、3 つのスレッドがセマフォに入ることができるようにします。各スレッドは、動作をシミュレートするために System.Threading.Thread.Sleep メソッドを使用して 1 秒間待機し、その後 Release メソッド オーバーロードを呼び出してセマフォを解放します。
セマフォが解放されるたびに、前のセマフォのカウントが表示されます。コンソール メッセージは、セマフォの使用状況を追跡します。出力を読み取りやすくするために、シミュレートされた動作間隔がスレッドごとに若干増加します。
Imports System Imports System.Threading Public Class Example ' A semaphore that simulates a limited resource pool. ' Private Shared _pool As Semaphore ' A padding interval to make the output more orderly. Private Shared _padding As Integer <MTAThread> _ Public Shared Sub Main() ' Create a semaphore that can satisfy up to three ' concurrent requests. Use an initial count of zero, ' so that the entire semaphore count is initially ' owned by the main program thread. ' _pool = New Semaphore(0, 3) ' Create and start five numbered threads. ' For i As Integer = 1 To 5 Dim t As New Thread(New ParameterizedThreadStart(AddressOf Worker)) 'Dim t As New Thread(AddressOf Worker) ' Start the thread, passing the number. ' t.Start(i) Next i ' Wait for half a second, to allow all the ' threads to start and to block on the semaphore. ' Thread.Sleep(500) ' The main thread starts out holding the entire ' semaphore count. Calling Release(3) brings the ' semaphore count back to its maximum value, and ' allows the waiting threads to enter the semaphore, ' up to three at a time. ' Console.WriteLine("Main thread calls Release(3).") _pool.Release(3) Console.WriteLine("Main thread exits.") End Sub Private Shared Sub Worker(ByVal num As Object) ' Each worker thread begins by requesting the ' semaphore. Console.WriteLine("Thread {0} begins " _ & "and waits for the semaphore.", num) _pool.WaitOne() ' A padding interval to make the output more orderly. Dim padding As Integer = Interlocked.Add(_padding, 100) Console.WriteLine("Thread {0} enters the semaphore.", num) ' The thread's "work" consists of sleeping for ' about a second. Each thread "works" a little ' longer, just to make the output more orderly. ' Thread.Sleep(1000 + padding) Console.WriteLine("Thread {0} releases the semaphore.", num) Console.WriteLine("Thread {0} previous semaphore count: {1}", _ num, _ _pool.Release()) End Sub End Class
using System; using System.Threading; public class Example { // A semaphore that simulates a limited resource pool. // private static Semaphore _pool; // A padding interval to make the output more orderly. private static int _padding; public static void Main() { // Create a semaphore that can satisfy up to three // concurrent requests. Use an initial count of zero, // so that the entire semaphore count is initially // owned by the main program thread. // _pool = new Semaphore(0, 3); // Create and start five numbered threads. // for(int i = 1; i <= 5; i++) { Thread t = new Thread(new ParameterizedThreadStart(Worker)); // Start the thread, passing the number. // t.Start(i); } // Wait for half a second, to allow all the // threads to start and to block on the semaphore. // Thread.Sleep(500); // The main thread starts out holding the entire // semaphore count. Calling Release(3) brings the // semaphore count back to its maximum value, and // allows the waiting threads to enter the semaphore, // up to three at a time. // Console.WriteLine("Main thread calls Release(3)."); _pool.Release(3); Console.WriteLine("Main thread exits."); } private static void Worker(object num) { // Each worker thread begins by requesting the // semaphore. Console.WriteLine("Thread {0} begins " + "and waits for the semaphore.", num); _pool.WaitOne(); // A padding interval to make the output more orderly. int padding = Interlocked.Add(ref _padding, 100); Console.WriteLine("Thread {0} enters the semaphore.", num); // The thread's "work" consists of sleeping for // about a second. Each thread "works" a little // longer, just to make the output more orderly. // Thread.Sleep(1000 + padding); Console.WriteLine("Thread {0} releases the semaphore.", num); Console.WriteLine("Thread {0} previous semaphore count: {1}", num, _pool.Release()); } }
#using <System.dll> using namespace System; using namespace System::Threading; public ref class Example { private: // A semaphore that simulates a limited resource pool. // static Semaphore^ _pool; // A padding interval to make the output more orderly. static int _padding; public: static void Main() { // Create a semaphore that can satisfy up to three // concurrent requests. Use an initial count of zero, // so that the entire semaphore count is initially // owned by the main program thread. // _pool = gcnew Semaphore( 0,3 ); // Create and start five numbered threads. // for ( int i = 1; i <= 5; i++ ) { Thread^ t = gcnew Thread( gcnew ParameterizedThreadStart( Worker ) ); // Start the thread, passing the number. // t->Start( i ); } // Wait for half a second, to allow all the // threads to start and to block on the semaphore. // Thread::Sleep( 500 ); // The main thread starts out holding the entire // semaphore count. Calling Release(3) brings the // semaphore count back to its maximum value, and // allows the waiting threads to enter the semaphore, // up to three at a time. // Console::WriteLine( L"Main thread calls Release(3)." ); _pool->Release( 3 ); Console::WriteLine( L"Main thread exits." ); } private: static void Worker( Object^ num ) { // Each worker thread begins by requesting the // semaphore. Console::WriteLine( L"Thread {0} begins and waits for the semaphore.", num ); _pool->WaitOne(); // A padding interval to make the output more orderly. int padding = Interlocked::Add( _padding, 100 ); Console::WriteLine( L"Thread {0} enters the semaphore.", num ); // The thread's "work" consists of sleeping for // about a second. Each thread "works" a little // longer, just to make the output more orderly. // Thread::Sleep( 1000 + padding ); Console::WriteLine( L"Thread {0} releases the semaphore.", num ); Console::WriteLine( L"Thread {0} previous semaphore count: {1}", num, _pool->Release() ); } };
import System.*; import System.Threading.*; public class Example { // A semaphore that simulates a limited resource pool. // private static Semaphore _pool; // A padding interval to make the output more orderly. private static int _padding; public static void main(String[] args) { // Create a semaphore that can satisfy up to three // concurrent requests. Use an initial count of zero, // so that the entire semaphore count is initially // owned by the main program thread. // _pool = new Semaphore(0, 3); // Create and start five numbered threads. // for (int i = 1; i <= 5; i++) { System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(Worker)); // Start the thread, passing the number. // t.Start((Int32)i); } // Wait for half a second, to allow all the // threads to start and to block on the semaphore. // System.Threading.Thread.Sleep(500); // The main thread starts out holding the entire // semaphore count. Calling Release(3) brings the // semaphore count back to its maximum value, and // allows the waiting threads to enter the semaphore, // up to three at a time. // Console.WriteLine("main thread calls Release(3)."); _pool.Release(3); Console.WriteLine("main thread exits."); } //main private static void Worker(Object num) { // Each worker thread begins by requesting the // semaphore. Console.WriteLine("Thread {0} begins " + "and waits for the semaphore.", num); _pool.WaitOne(); // A padding interval to make the output more orderly. int padding = Interlocked.Add(_padding, 100); Console.WriteLine("Thread {0} enters the semaphore.", num); // The thread's "work" consists of sleeping for // about a second. Each thread "works" a little // longer, just to make the output more orderly. // System.Threading.Thread.Sleep(1000 + padding); Console.WriteLine("Thread {0} releases the semaphore.", num); Console.WriteLine("Thread {0} previous semaphore count: {1}", num, (Int32)_pool.Release()); } //Worker } //Example

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Semaphore.Release メソッド
Weblioに収録されているすべての辞書からSemaphore.Releaseを検索する場合は、下記のリンクをクリックしてください。

- Semaphore.Releaseのページへのリンク