当前位置:网站首页>C thread pool control semaphore

C thread pool control semaphore

2022-06-26 00:53:00 App Hunter

Purpose : Control the number of threads to execute some code 、 Avoid swarming into The asynchronous execution of causes the system to collapse .

Use :

1.       

Semaphore Semaphore = new
        Semaphore(3, 3);  // Number of control threads

2.

 public void StatisticsDate(object store){

Semaphore.WaitOne();// Number of thread pool control entries

// Code to execute

Semaphore.Release();// End current thread pool control

}

 

3. So the thread enters StatisticsDate The method will Execute one by one according to the specified quantity .

4. need Static class processing Semaphore, Convenient for global control .

5.Semaphore timeout handler

if (Semaphore.WaitOne(1000))
{
    //  The processing unit without timeout is milliseconds 
}
else
{
    //  Waiting for timeout processing 
}
原网站

版权声明
本文为[App Hunter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252244286999.html