Project
Create Projects
- Server solution
- Create console app and name solution to
Server
- Create project in
Server
solution namedDummyClient
andServerCore
- set
ServerCore
project to starting project
- Create console app and name solution to
Thread
Create Thread
- ServerCore\Program.cs
- you have to make main thread function
- connect main thread function
Start
lets main thread function start
Foreground Thread
- default is foreground thread in C#
-
if main thread has infinity funtion, then it will not finish forever
- ServerCore\Program.cs
Background Thread
- you can change main thread to background thread by
IsBackgound
method -
even there is infinity function in main thread, this thread will be finished because it is background thread
- ServerCore\Program.cs
Waiting Thread
- main can wait our main thread by
Join
Method -
you can define Thread name by
Name
Method - ServerCore\Program.cs
ThreadPool
- Thread is already made in C#
- So when we need this thread, just use
ThreadPool
-
after their work, they are not removed, just changed to deactivate
- ServerCore\Program.cs
Set number of Threads
- you can set number of threads by
SetMinThreads
andSetMaxThreads
Test: existing waiting thread
-
if there is waiting thread, then this thread will excute
-
ServerCore\Program.cs
Test: non existing waiting thread
-
but if there is no waiting thread because of infinity function in thread, then this project cannot be finished
-
ServerCore\Program.cs
Task
- Task is action
- Task is work of Thread
-
you can define this thread is infinity by
LongRunning
- ServerCore\Program.cs
Compiler Optimization
- you can make stop duration by
Sleep
Wait
is same function withJoin
, butWait
is for Task andJoin
is for Thread-
you can wait main Task by
Wait
- ServerCore\Program.cs
Release Mode
- release mode optimize our code
- So
while
method is optimized by Release mode - and this makes problem(like infinity program)
Disassembler
-
you can check your C# code to assembly language
-
Disassember
- make stop point in while method
- excute program by debugging mode
- click [Debug]-[Window]-[Disassembler]
- Result
- by optimization, while method became infinity method
Volatile
-
you can avoid optimization by
volatile
-
ServerCore\Program.cs
Cache
- Temporal Locality
- recently used memory must have be used again
- Spacial Locality
- close memory with recently used memory must have be used
Test: Spacial Locality
- ServerCore\Program.cs
Memory Barrier
Hardware Optimization
- if there is no related with each code, ordering of code can be changed
Test
- ServerCore\Program.cs
- this mean,
r1=x
is ordered first theny=1
andr2=y
is ordered first thenx=1
Memory Barrier
-
To avoid Hardware optimization
- Memory Barrier
- avoid relocating code
- Visibility
- Full memory Barrier(ASM MFENCE, C# Thread.MemoryBarrier)
- ban Store and Load
- Store Memory Barrier (ASM SFENCE)
- ban only Store
- Load Memory Barrier(ASM LFENCE)
- ban only Load
Memory Barrier timing
- Store
- after store
- Load
- before load
Test
- ServerCore\Program.cs
Interlocked
Race Condition
- Tasks excute withour order
Test
- ServerCore\Program.cs
- this happened because
number++
andnumber--
is not atomic
Interlocked
- make method to atomic
Test
- ServerCore\Program.cs
- result is changed because
Interlocked.Increment
andInterlocked.Decrement
is atomic
Lock
Mutual Exclusive
- Monitor
- if code is too long to make interlocked, we can use
Monitor
Enter
means lock the task andExit
means release the task- after
Exit
, other tasks are running
- if code is too long to make interlocked, we can use
Test
- ServerCore\Program.cs
Lock
- Lock
Lock
is same function with try-catch-finally- object is used like a Lock
Test
- ServerCore\Program.cs
DeadLock
- if there are two keys and two process and each process got one key, then this program never gonna finish
Test
- ServerCore\Program.cs
- Sleep
- you can stop one of thread untill other thread gonna be finished
Test
- ServerCore\Program.cs
SpinLock
- one thread waits other thread untill this thread gonna be finished
Test
- ServerCore\Program.cs
-
this happened because this process is not atomic
-
Interlocked.Exchange
Test
- ServerCore\Program.cs
- Interlocked.CompareExchange
Test
- ServerCore\Program.cs
Context Switching
- Thread.Sleep(1);
- an unconditional rest
- Monitor and lock
- Thread.Sleep(0);
- an unconditial yield
- but thread cannot yield to lower priority
- if there is only same or lower priority with current thread, then current thread is excuted
- SpinLock
- Thread.Yield();
- a generous concession
- if there is excutable thread, then this thread is excuted
- if threr is not excutable thread, remained time is removed
- Mutex
ResetEvent
- AutoResetEvemt
- speed is really not good, but thread is excuted automatically
Test
- ServerCore\Program.cs
Mutex
- Mutex
- Mutex can count wait and release, and use threadId
Test
- ServerCore\Program.cs
ReaderWriterLock
- in normal situation, there is no lock
- but if in specific situation, for example, you want to write, then lock is excuted
Test : Non Recursive Lock
- ServerCore\Lock.cs
- Spin Lock Policy: 5000 times → Yield
Test : Recursive Lock
- ServerCore\Lock.cs
- Spin Lock Policy: 5000 times → Yield
- WriteLock → WriteLock OK, WriteLock → ReadLock OK, ReadLock → WriteLock NO
- ServerCore\Program.cs
Thread Local Storage
- Global variables that are uniquely accessible
Test
- ServerCore\Program.cs