System::Threading::Mutex 클래스

Mutex class

Mutex implemnetation. Objects of this class should only be allocated using System::MakeObject() function. Never create instance of this type on stack or using operator new, as it will result in runtime errors and/or assertion faults. Always wrap this class into System::SmartPtr pointer and use this pointer to pass it to functions as argument.

class Mutex : public System::Threading::WaitHandle

메서드

메서드설명
Mutex()RTTI 정보.
Mutex(bool)생성자.
Mutex(bool, const String&)생성자.
ReleaseMutex()뮤텍스를 해제합니다.
static Remove(const String&)시스템에서 지정된 이름의 뮤텍스를 삭제합니다.
virtual Reset()뮤텍스 상태를 재설정합니다. 구현되지 않음.
virtual Set()뮤텍스를 신호 상태로 설정합니다. 구현되지 않음.
WaitOne() override뮤텍스를 잠급니다. 필요시 무제한 대기를 수행합니다.
WaitOne(int) override뮤텍스를 잠급니다. 필요시 대기를 수행합니다.
WaitOne(TimeSpan) override뮤텍스를 잠급니다. 필요시 대기를 수행합니다.

필드

필드설명
static WaitTimeout함수가 반환할 특수 값이며, 타임아웃이 초과되고 아무 것도 신호를 보내지 않을 경우 배열에서 신호된 객체의 인덱스를 반환합니다.

비고

#include "system/threading/mutex.h"
#include "system/threading/thread.h"
#include "system/console.h"
#include "system/convert.h"
#include "system/smart_ptr.h"
#include "system/string.h"

int main()
{
  auto mutex = System::MakeObject<System::Threading::Mutex>();

  System::String str;

  const int THREADS_COUNT = 3;
  std::vector<System::SharedPtr<System::Threading::Thread>> threads;
  threads.reserve(THREADS_COUNT);

  for (auto i = 0; i < THREADS_COUNT; ++i)
  {
    threads.push_back(System::MakeObject<System::Threading::Thread>([&mutex, &str]()
    {
      mutex->WaitOne();

      str += u"Thread " + System::Convert::ToString(System::Threading::Thread::GetCurrentThreadId()) + u" started." + System::Environment::get_NewLine();

      System::Threading::Thread::Sleep(200);

      str += u"Thread " + System::Convert::ToString(System::Threading::Thread::GetCurrentThreadId()) + u" ended." + System::Environment::get_NewLine();

      mutex->ReleaseMutex();
    }));

    threads[i]->Start();
  }

  System::Threading::Thread::Sleep(700);

  System::Console::WriteLine(str);

  return 0;
}
/*
This code example produces the following output:
Thread 1 started.
Thread 1 ended.
Thread 2 started.
Thread 2 ended.
Thread 3 started.
Thread 3 ended.
*/

또 보기