Note---Summary of Synchronization between Multithreads

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 01:28   34   0

the following items are some of methods which i collected about the synchronization between the multithreads.

volatile

decorate the variable with the key word volatile, which is only for the reference type and some special primitive type

Interlocked

  • Interlocked.Increment(ref para);
  • Interlocked.Decrement(ref para)

this mechanism is valid only when the para is int or long type.

Monitor

the function Monitor.Enter() and Monitor.Exit()

Monitor.TryEnter(), this is similar to the Enter(), the only difference is if the resource trying to access is already locked by another thread, the Enter() waits until geting the right or timeout but the TryEnter() just returns false.

Lock

lock is an brief expression of Monitor.Enter()(beginning of lock) and Monitor.Exit() (end of lock)

Monitor.

Wait(), wait will hold the current thread until the locked object call the Pulse();

Monitor.Pulse() ,this method is used to tell the Wait() ;

Monitor.PulseAll(),;

Mutex: WaitOne, ReleaseMutex. they are just like the Monitor.Enter() and Monitor.Exit(), but Mutex is a advanced one which is designed for across the process boundary while the Monitor is not. if there is no need for acorss prpcess bounday, the best pratice is to use the Monitor mechanism which is designed for best performance.

EventWaitHandler

ManualResetEvent ,AutoResetEvent ,the difference between those two is that the first needs to call the Reset() to set the event to the unactivated state. the most important methods are Set(), WaitOne() and Reset();

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP