Thread Learning [closed]
Just trying to ping up some experienced Thread gurus out there...trying to learn more about threading without many real code issues at the moment that would help me practice it more.
I've got some resources already but probably there are some good ones I do not know of out there:
- C# 2008 and 2005 Threaded Programming: Beginner's Guide (Book) (this is the one I'm thinking about starting to look at first)
- CLR via C# (Dev-Pro) (I have this book also and Jeffery has some threading stuff in here too)
- Threading in C# - eBook
- Concurrent Programming on Windows
- .NET Multi-threading Book from 2003 (Book) (not sure if this stuff is outdated...)
So:
1) Looking for others (videos, books, etc.) and also what your opinion is on the resources above (would you recommend certain ones as better or ones as outdated / bad?).
2) Also, since this is a broad and highly complex topic...being a complete foreigner to this, I'm trying to narrow down some kind of sane study list. Threading is deeeeeeeep so I just want to cover the basics or most likely the areas they'll probably touch (locking, thread safe, etc.). I don't need to go into this interview being an expert...just show that I am not completely clueless about when to use it, common scenarios and a couple examples I can ex开发者_开发知识库plain to them.
3) Anyone with threading experience out there to point me to some good resources or give me some good study tips or areas you think I should most definitely hit up?
4) Obviously with the advent of .NET 4.0 that changes things altogether but I am focusing on .NET 3.5 mainly here. I need to just know the basics of THREADING first...the concept, things to be aware of, locking, thread safe singletons (I know John Skeet's page on Singletons, etc.).
5) Another thing that would help me are real world examples and reasons to use threading. Both on the IIS side, OOP side, server farms, ect. I'd like to know some real examples of use of threading or common scenarios in real life apps where you really need to start utilizing threading.
I don't need everyone to answer all 5 of my areas here...even just advice in one of the 1-5 above would be greatly appreciated.
Thanks...
UPDATE / DISCLAIMER (after receiving a few assumptions about me)
No I'm not going to go in there claiming I know threading or am an expert in it, but that I'm a beginner and have done only a couple things at a past job and still learning. But I did want to give the context in why I'm posting this entire thread here so you can help me hone in better in a shorter period of time...filter out a path that may be better so I don't waste my time both for the interview but also for my future learning.
Threading can seem very easy to learn. You just use new Thread(ThreadFunc).Start()
and voila, you are using threading. How hard can it be, right?
Threading problems are not something that you will notice directly but are something that will sneak up when you least expect it. They usually come in production systems when there are some load and sporadically shut down your system.
After a while you will discover that you need to do synchronizing before modifying share data. And when you start doing so, you will see a large performance degrade.
Threading is easy to get going with, but will take years to master. Protecting shared data is an art and will need a lot of thought to get it done properly. I've written multi threaded servers for several years, both in C++ with IO completion ports and lately in C#. I still make mistakes that can produce unexpected result in my servers.
As for topics you need to learn:
- Look at some examples using
Thread
class. - The
lock
keyword. - Asynchronous programming (it's very easy to do this wrong, just look at all SO questions about it). .Net uses BeginXXXX/EndXXXX for async programming.
- Other locking techniques than lock, for instance
Semaphore
andReaderWriterLock
- The
Interlocked
class.
I usually use this pattern for Thread
functions:
public class Example
{
ManualResetEvent _myEvent = new ManualResetEvent(false);
Thread _myThread;
bool _isRunning = true;
public Example()
{
_myThread = new Thread(WorkerFunc);
_myThread.Start();
}
public void WorkerFunc()
{
while (_isRunning)
{
try
{
_myEvent.Wait(Timeout.Infinite);
_myEvent.Reset();
ActualFunc();
}
catch (ThreadAbortException) { return; }
catch (Exception err)
{
_logger.Error("Thread func failed, lucky we caught it so the server don't die..", err);
}
}
}
public void Stop()
{
_isRunning = false;
_myEvent.Set();
_myThread.Join();
}
public void DoWork()
{
//add some work to the thread job queue or something
_myEvent.Set();
}
private void ActualFunc()
{
//actual thread work is done here
// in a seperate method to keep everything clean.
}
}
Just wrote it here in the answer, do not promise that it will work 100% :)
The nice introduction to Threading http://www.albahari.com/threading/ ,Although new threading patterns and techniques introduced in .NET 4.0 and even in C# 5.0 preview, it would be a nice introduction for diving into topic.
You also should read about Thread Pools.
精彩评论