惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - DotCat

幽默的David Solomon MSDN上ReportViewer的两个代码下载地址 Converting RDL and RDLC Files VS打SP1后reportviewer用custom assembly的变化 Meet 牛人 @ April: Jeffrey Richter & David Solomon 当你聊天你会想起谁--Vista Sidebar Gadget DIY - DotCat 哈,IE7!--dudu的CNBlogs DotText 1.0 Beta 2中FreeTextBox对IE7的不兼容性 - DotCat CCS bug之8: 有关设置精华帖的操作在帖子的操作历史里浏览出错 - DotCat CCS bug之7: 设置精华帖后在精华区不能看到 [CodeProject每日一荐]实现Double Metaphone语音匹配算法[三,四] VBScript调用COM;存储过程实现及高级话题 MSN和Windows Live Messenger机器人大赛 [CodeProject每日一荐]实现Double Metaphone语音匹配算法[二]:Visual Basic的COM实现和关系数据库解决方案 [CodeProject每日一荐]实现Double Metaphone语音匹配算法[一]:介绍与C++实现 [CodeProject每日一荐] 基于xml的在线选择题小测试(调查问卷) 解决问题: .Net异常 Method not found: Void System.Web.UI.WebControls.BaseDataList.set_Caption(System.String). 解决问题: sql server 2000 企业管理器打不开了 推荐解除文件锁定软件: unlocker CuteChat3.0 (for CommunityServer2.0) 发布了 CommunityServer 2.1 的消息
.NET threading in C# Training Highlights sharing
DotCat · 2007-04-20 · via 博客园 - DotCat

Jeffery’s training: .NET Threading in C#: Building Responsive, Reliable & Scalable Code

I’d like to share some highlights with you. And the demo code is here: www.wintellect.com/2397

1. Introduction

  • Early OS had just 1 thread, and then Windows supports multiple threads for robustness instead of performance.
    • n Bad products use lots of threads but most of the time doing nothing (low CPU usage). For example: Movie Maker, Outlook and Visual Studio
  • Threads are overhead
    • Kernel objects in CPU registers: x86=~700bytes, x64=~1240bytes,IA64=~2500bytes
    • User-mode stack: 1MB committed
    • Kernel-mode stack:12KB/24KB
    • For parameter/local var, exception-handling chain: …
    • Dll attach/detach notifications: hundreds of functions are called
  • Window pre-empts running thread and schedules another (non-waiting) thread each quantum (~20ms), which is called a context switch. Every context switch requires that Windows
    • Enter kernel mode (very expensive)
    • Save registers from CPU to running thread’s kernel object (x86=~700bytes, x64=~1240bytes,IA64=~2500bytes)
    • Determine which thread to schedule next. If thread owned by other process, switch address space (very expensive)
    • Load register from selected thread’s kernel object into CPU
    • Leave kernel mode.
  • Conclusion
    • avoid threads: they are time/memory overhead
    • use threads: they enable robustness & scalability on multi-CPU system
  • Using thread
    • Framework class library has 2 namespaces for process/thread
    • Discourage: System.Diagnostics(Process & ProcessThread), providing Windows’view
    • Encourage: System.Threading(Thread), providing CLR managed view
  • Constructing one thread doesn't have Windows create a thread. Instead, Start() does.
  • Sleep(Timespan timeout) suspends a thread at least timeout timespan.
    • Every thread has a base priority number, 0(lowest & reversed) to 31(highest), Windows boosts a thread’s priority when an event occurs.
    • Windows Thread vs CLR Thread
      • Thread’s ManagedThreadId in CLR differs from ProcessThread’s Id in Windows
      • Some properties are only in Windows, such as ProcessorAffinity, useful to assign task to a specific processor.
      • Some properties are only in CLR, such as IsBackground(true if thread won’t force app to stay alive)
    • Threading support in VS
      • Thread window
      • You can select which thread to step through
      • You can freeze/thaw threads to take them out of the picture

2. Performing Asynchronous Operations with the CLR’s Thread Pool

  • There is one ThreadPool per process, shared by all AppDomains.
  • If work items queue quickly, more threads are created; If work items reduce, a ThreadPool thread waits 2 minutes, wake and kills itself.
  • Thread pools should never have a max thread limit
  • There are mainly 2 kinds of task need thread
    • Compute-Bound
      • When to create your own thread instead of using threadpool
        • Non-normal thread priority
        • Foreground thread to keep app alive until work item is done
        • Long-running compute-bound task
        • Aborting the operation
      • Periodically performing asynchronous compute-bound operation: System.Threading.Timer. 2 more timers:
        • System.Windows.Forms.Timer: all work is done by just 1 thread
        • System.Timers.Timer: System.Threading.Timer ‘s wrapper to support VS design time operation.
    • I/O-Bound

3. The CLR’s Asynchronous Programming Model

  • CLR creates 1 IOCP per process and creates thread pool threads that wait on the IOCP.
  • Standard pattern to execute asynchronous operations
    • I/O-bound: BeginXXX for many classes, such as Stream, Dns, Socket, WebRequest, SqlCommand
    • Computer-bound: BeginInvoke
  • If you don't’ specify the FileOptions.Asynchronous flag, Windows performs synchronous operations, which is very inefficient.
  • Windows always performs some File I/O operations synchronously: Compression, Extending a file’s length by appending, Cached data.
  • APM supports 3 rendezvous techniques
    • Wait until done: BeginXXX and then EndXXX
    • Polling: BeginXXX, and then call IAsyncResult.IsCompleted repeatedly, and then EndXXX
    • Callback: 
      • I/O-bound: BeginXXX, ThreadPool thread calls device, ThreadPool thread back to ThreadPool, … callback, EndXXX
      • Compute-bound: BeginInvoke, ThreadPool thread works, EndInvoke
  • Exception 
    • BeginXXX throw exception: no item queued
    • Operation fails: EndXXX will throw exception
  • APM Usage Note
    • Must call EndXXX or you will leak resources.
    • Just call EndXXX once or result is unpredictable.
    • Call EndXXX with the same object used by BeginXXX or you will get InvalidOperationException
    • Many Win32 API don’t offer async versions
  • WinForm and WPF GUI only can be updated in thread who creates the windows. This is not APM
  • WebForm can be configured as async rendering in ASP.NET 2.0: Async=”true” as page directive in aspx file and some BeginXXX, EndXXX methods.

4. The Event-Based Asynchronous Programming Model

5. Thread Synchronization

  • Thread safety
    • All static Framework Class library are thread safe
    • Framework Class library Instance methods are usually not thread safe.
    • .NET Fx v1.x collections had confusing thread-safe wrappers, which have been removed in .NET Fx 2.0 (ArrayList.Synchronized(new ArrayList());) 
  •  Code optimization by c#/JIT complier and CPU reordering may execute your code out of program order.
    • VolatileRead/VilotileWrite/memory barrier(memory fence)
    • Avoid using volatile keyword
  • Manipulate an Int32 in a thread-safe way: Interlock
  • Monitor, lock
    • Double-check locking technique
  • ReaderWriterLock
  • Window Kernel Objects 
    • System.Threading.WaitHandle (Mutex,Semaphore, EventWaitHandle(AutoResetEvent/ManualResetEvent))
    • ThreadPool offers an efficiend way to execute a method when a kernel object becomes signaled:ThreadPool.RegisterWaitForSingleObject
  • Conclusion
    • Always do async I/O, because sync I/O blocks a thread.
    • Avoid using a thread for a dedicated task, because it blocks unless task runs at 100% CPU utilization continuously.
    • Avoid using thread synchronization constructs, because they can block a thread.
    • Use volatile Reads/Writes & Interlocked methods, because they are extremely fast, never block a thread.

6. The Art of Thread Synchronization. Referring to his book and free power threading library

7. Aborting a Thread