C\#중 다 중 스 레 드 Manual ResetEvent 와 AutoResetEvent 의 차이
공통점:
모두 EventWaitHandle 인 터 페 이 스 를 계승 하기 때문에 다음 과 같은 기능 을 가진다.
리 셋()//빨 간 불
Set()//그린 라이트
WaitOne()//대기 신호
다른 점:
AutoResetEvent 가 Set 을 받 은 후 한 번 에 하나의 스 레 드 만 실행 할 수 있 고 다른 스 레 드 는 계속 WaitOne 입 니 다.
Manual ResetEvent 가 Set 을 받 은 후 모든 처리 WaitOne 상태 스 레 드 가 계속 실 행 됩 니 다.
msdn 은 다음 과 같이 언급 했 습 니 다.(스 레 드 가 WaitOne()상태 에 있 지 않 고 Set 을 호출 하면 AutoResetEvent 는 Set 상 태 를 유지 합 니 다)
Set 신호 자동 ResetEvent 를 호출 하여 대기 스 레 드 를 방출 합 니 다.AutoResetEvent 는 스 레 드 가 풀 릴 때 까지 종료 상 태 를 유지 하고 비 신호 상태 로 자동 으로 돌아 갑 니 다.만약 스 레 드 가 대기 상태 에 있 지 않 으 면,상 태 는 이미 보 낸 신 호 를 무기한 으로 유지 할 것 이다.
따라서 보통 WatiOne 전에 Reset()을 한 번 하고 Set 신 호 를 지 웁 니 다.
주의해 야 할 것 은(두 개의 Set 호출 사이 의 시간 이 비교적 짧 고 두 번 째 Set 신 호 를 잃 어 버 릴 수 있 기 때문에 연속 Set 호출,중간 에 Sleep 일정 시간 이 필요 합 니 다):
보증 할 수 없 는 모든 호출 Set 방법 은 하나의 스 레 드 를 방출 합 니 다.두 번 째 호출 이 너무 가 까 워 서 두 번 째 호출 전에 스 레 드 가 발생 할 수 있 도록 한 스 레 드 만 풀 려 납 니 다.두 번 째 호출 이 일어나 지 않 은 것 처럼.또한 Set 에서 기다 리 지 않 은 스 레 드 호출 과 AutoResetEvent 가 종료 되면 호출 이 작 동 하지 않 습 니 다.
한 네티즌 은"
AutoResetEvent.Set() = ManualResetEvent.Set() + ManualResetEvent.Reset();
개인 적 으로 이것 은 원리 차원 의 의미 일 뿐 실제 사용 과정 에서 차이 가 있 는 것 은 다음 과 같다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testManualResetEvent
{
class Program
{
static object objManualResetEvent = new object();
static System.Threading.ManualResetEvent manu = new System.Threading.ManualResetEvent(false);
//static System.Threading.AutoResetEvent manu = new System.Threading.AutoResetEvent(false);
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(() => { Product(); }));
t.Start();
}
manu.Set();
manu.Reset();
Console.ReadKey();
}
static void Product()
{
manu.WaitOne(10000);
Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
}
}
}
실제 실행 결 과 는 set 후 reset 를 실행 하기 전에 몇 개의 스 레 드 가 실행 을 불 러 일 으 키 는 지 예측 할 수 없습니다.한 번 에 하나의 라인 을 통과 할 수 있 도록 자 물 쇠 를 추가 해 야 합 니 다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testManualResetEvent
{
class Program
{
static object objManualResetEvent = new object();
static System.Threading.ManualResetEvent manu = new System.Threading.ManualResetEvent(false);
//static System.Threading.AutoResetEvent manu = new System.Threading.AutoResetEvent(false);
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(() => { Product(); }));
t.Start();
}
manu.Set();
//System.Threading.Thread.Sleep(100); // set sleep
//manu.Set();
//manu.Reset();
//System.Threading.Thread.Sleep(100);
//manu.Set();
//manu.Reset();
Console.ReadKey();
}
static void Product()
{
lock (objManualResetEvent)
{
manu.WaitOne(10000); manu.Reset();
Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
}
}
}
}
실행 결과:C\#중 Manual ResetEvent 와 AutoResetEvent 의 차이 에 관 한 이 글 은 여기까지 소개 합 니 다.더 많은 관련 C\#중 Manual ResetEvent AutoResetEvent 내용 은 저희 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.