473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Threading in C# - Article (free)

I've written an extensive document on threading in C#:

http://www.albahari.com/threading

It started out as a chapter in a book that I'm writing, but ended up
outgrowing the book! So I'm making it free and maintaining it in
parallel as a separate project.

Any feedback would be welcome.

I'm particularly interested in comments in regard the Wait and Pulse
section. I'm proposing a design pattern that makes Wait and Pulse as
simple to use as an AutoResetEvent (well, almost!)

Also, I've come down rather hard on synchronization contexts, I think,
fairly! But if you can convince me otherwise, a u-turn is not out of
the question. It has to be a good argument, though!

Thanks,
Joe

May 17 '06 #1
13 5061
Check email, I've send several ideas privately
I've written an extensive document on threading in C#:

http://www.albahari.com/threading

It started out as a chapter in a book that I'm writing, but ended up
outgrowing the book! So I'm making it free and maintaining it in
parallel as a separate project.

Any feedback would be welcome.

I'm particularly interested in comments in regard the Wait and Pulse
section. I'm proposing a design pattern that makes Wait and Pulse as
simple to use as an AutoResetEvent (well, almost!)

Also, I've come down rather hard on synchronization contexts, I think,
fairly! But if you can convince me otherwise, a u-turn is not out of
the question. It has to be a good argument, though!

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

May 17 '06 #2
Awesome article Joe. I have added it to my extensive collection of favorites
and plan to direct many people to it for help. It is nicely-layed-out, uses
no more than a minimum of code for examples (which aids in not confusing the
issue), and well-written.

Kudos!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

<jo************@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
I've written an extensive document on threading in C#:

http://www.albahari.com/threading

It started out as a chapter in a book that I'm writing, but ended up
outgrowing the book! So I'm making it free and maintaining it in
parallel as a separate project.

Any feedback would be welcome.

I'm particularly interested in comments in regard the Wait and Pulse
section. I'm proposing a design pattern that makes Wait and Pulse as
simple to use as an AutoResetEvent (well, almost!)

Also, I've come down rather hard on synchronization contexts, I think,
fairly! But if you can convince me otherwise, a u-turn is not out of
the question. It has to be a good argument, though!

Thanks,
Joe

May 17 '06 #3
Joe,

That's an excellent article. I haven't had a chance to analyze it in
detail, but overall I like the layout and it is well written.

You might at least mention Thread.BeginThreadAffinity and the
Thread.BeginCriticalRegion in the advanced section. I'd like to see a
quality discussion on these two methods. I don't think we've ever
discussed them in this forum yet.

Brian
jo************@gmail.com wrote:
I've written an extensive document on threading in C#:

http://www.albahari.com/threading

It started out as a chapter in a book that I'm writing, but ended up
outgrowing the book! So I'm making it free and maintaining it in
parallel as a separate project.

Any feedback would be welcome.

I'm particularly interested in comments in regard the Wait and Pulse
section. I'm proposing a design pattern that makes Wait and Pulse as
simple to use as an AutoResetEvent (well, almost!)

Also, I've come down rather hard on synchronization contexts, I think,
fairly! But if you can convince me otherwise, a u-turn is not out of
the question. It has to be a good argument, though!

Thanks,
Joe


May 17 '06 #4
Thanks for the support!
You might at least mention Thread.BeginThreadAffinity and the
Thread.BeginCriticalRegion in the advanced section. I'd like to see a
quality discussion on these two methods. I don't think we've ever
discussed them in this forum yet.


I'm reluctant to comment on these methods until I can find a practical
use for them (ie what problems they will solve, excluding the obscure
or contrived). If BeginCriticalRegion prevented a section of code from
being aborted it would definitely be useful. But this is not the case,
at least in the standard CLR hosting environment. Under the SQL CLR
host, the rules are different, but then again a SQL CLR method is
unlikely to *need* thread affinity, nor to be executing critical code,
especially given the restrictions of the recommended safe permission
set.

Has anyone had any experience with BeginThreadAffinity or
BeginCriticalRegion?

Regards
Joe

May 18 '06 #5

<jo************@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
| Thanks for the support!
|
| > You might at least mention Thread.BeginThreadAffinity and the
| > Thread.BeginCriticalRegion in the advanced section. I'd like to see a
| > quality discussion on these two methods. I don't think we've ever
| > discussed them in this forum yet.
|
| I'm reluctant to comment on these methods until I can find a practical
| use for them (ie what problems they will solve, excluding the obscure
| or contrived). If BeginCriticalRegion prevented a section of code from
| being aborted it would definitely be useful. But this is not the case,
| at least in the standard CLR hosting environment. Under the SQL CLR
| host, the rules are different, but then again a SQL CLR method is
| unlikely to *need* thread affinity, nor to be executing critical code,
| especially given the restrictions of the recommended safe permission
| set.
|
| Has anyone had any experience with BeginThreadAffinity or
| BeginCriticalRegion?
|
| Regards
| Joe
|

While they sure relate to sophisticated hosts like SQL2005, these API's have
little to do with safe or unsafe SQL2005 permissions sets.
Say you have a managed class, that implements it's own sophisticated high
performing locking primitive (for instance a spinlock) instead of using one
of the primitives offered by the framework (who are calling these API's
appropriately) , well, such a class doesn't need unsafe SQL2005 context to
run. If you ever intend to use this class in a SQL2005 hosted application,
you'll have to inform the CLR that you are holding a managed lock (a shared
state) by calling BeginCriticalRegion, so that on his turn notifies the host
that it's unsafe to escalate a thread abort (or other asynchronous
exception) to a rude abort or rude AD unload, as long as the lock is held.
The same goes for BeginThreadAffinity, whenever you hold into shared state
that consists of thread affinitized types, you need to make sure that the
host gets informed. That way the host (say SQL2005) can take measures to
return on the same thread as when the host was entered.

Willy.
May 18 '06 #6

jo************@gmail.com wrote:
Thanks for the support!
You might at least mention Thread.BeginThreadAffinity and the
Thread.BeginCriticalRegion in the advanced section. I'd like to see a
quality discussion on these two methods. I don't think we've ever
discussed them in this forum yet.
I'm reluctant to comment on these methods until I can find a practical
use for them (ie what problems they will solve, excluding the obscure
or contrived).


I appreciate your reluctance. These methods are very specialized and
would have little benefit to most developers. But, it might be
beneficial to at least discourage their use. Some inexperienced
developer who discovers threading for the first time may feel the urge
to use them just because they look cool.
If BeginCriticalRegion prevented a section of code from
being aborted it would definitely be useful. But this is not the case,
at least in the standard CLR hosting environment. Under the SQL CLR
host, the rules are different, but then again a SQL CLR method is
unlikely to *need* thread affinity, nor to be executing critical code,
especially given the restrictions of the recommended safe permission
set.

Has anyone had any experience with BeginThreadAffinity or
BeginCriticalRegion?


I have no experience. To be honest I don't fully understand how they
work. Though, I do know that the Hashtable in 2.0 uses
Thread.BeginCriticalRegion. An interesting blog on the reasoning can
be found with the following link.

<http://blogs.msdn.com/bclteam/archive/2005/06/14/429181.aspx>

May 18 '06 #7
jo************@gmail.com writes:
Any feedback would be welcome.


Hello,

I am currently reading your article and I think it is very interesting
and extremely well written.

I however now have a question about section 3, especially your claim
that one MUST call EndInvoke() when using delegates with BeginInvoke().

I now re-read the MSDN section "Asynchronous Programming Using
Delegates" several times concerning BeginInvoke() but I cannot find an
evidence that I MUST call EndInvoke(). In fact, I have some applications
that never do so, they just use the BeginInvoke() method for a
"Fire-and-Forget" call.

Did I really introduce a resource-leek here?

Best regards,
Martin
May 18 '06 #8

"Martin Carpella" <ma*************@gmx.net> wrote in message
news:87************@msgid.carpella.net...
| jo************@gmail.com writes:
|
| > Any feedback would be welcome.
|
| Hello,
|
| I am currently reading your article and I think it is very interesting
| and extremely well written.
|
| I however now have a question about section 3, especially your claim
| that one MUST call EndInvoke() when using delegates with BeginInvoke().
|
| I now re-read the MSDN section "Asynchronous Programming Using
| Delegates" several times concerning BeginInvoke() but I cannot find an
| evidence that I MUST call EndInvoke(). In fact, I have some applications
| that never do so, they just use the BeginInvoke() method for a
| "Fire-and-Forget" call.
|
| Did I really introduce a resource-leek here?
|
| Best regards,
| Martin
From: http://msdn2.microsoft.com/en-us/library/2e08f6yc.aspx
...
Important
Always call EndInvoke to complete your asynchronous call.
...

Willy.


May 18 '06 #9
"Willy Denoyette [MVP]" <wi*************@telenet.be> writes:
From: http://msdn2.microsoft.com/en-us/library/2e08f6yc.aspx
..
Important
Always call EndInvoke to complete your asynchronous call.


Hmm, I see, I just looked up BeginInvoke() on the MSDN associated with
VS2005, should be mentioned there as well.

Ok, I'll be going code-fixing then...

Best regards,
Martin
May 18 '06 #10
On Thu, 18 May 2006 20:50:30 +0200, Martin Carpella
<ma*************@gmx.net> wrote:
Hmm, I see, I just looked up BeginInvoke() on the MSDN associated with
VS2005, should be mentioned there as well.

Ok, I'll be going code-fixing then...


There's one exception: you don't need EndInvoke for a
Control.BeginInvoke call because that's implemented differently (as a
Win32 PostMessage call).
--
http://www.kynosarges.de
May 19 '06 #11


Martin Carpella wrote:
Ok, I'll be going code-fixing then...


The callback is an *excellent* place to run EndInvoke :)

foo.BeginInvoke(args...,
delegate(IAsyncResult r) { foo.EndInvoke(r, args...); },
null);

Remember, if "foo" throws you have to decide what to do with the
exception when you EndInvoke.

--
Helge
May 19 '06 #12
Chris Nahr <ch************@kynosarges.de> writes:
There's one exception: you don't need EndInvoke for a
Control.BeginInvoke call because that's implemented differently (as a
Win32 PostMessage call).


Thanks, I found this difference on the web and I almost supposed it as
there is no possibility to add a callback in Control.BeginInvoke().

Unfortunately this won't save me from fixing up the existing code, as
most of it is part of a service logic which just uses the BeginInvoke()
for a typesafe implementation of a BackgroundWorker.

Thanks anyway & best regards,
Martin
May 19 '06 #13
Helge Jensen <he**********@slog.dk> writes:
The callback is an *excellent* place to run EndInvoke :)
Yes, I know, but I didn't want to write one for every "fire&forget" call
;)
foo.BeginInvoke(args...,
delegate(IAsyncResult r) { foo.EndInvoke(r, args...); },
null);
Using anonymous methods here is a nice idea, never thought of this one.
Too bad one cannot write a general handler for any delegate to be
BeginInvoke()d and EndInvoke()d, as one needs to know the exact type of
the delegate for doing this.
Remember, if "foo" throws you have to decide what to do with the
exception when you EndInvoke.


Of course. Even if I sometimes swear at it, the new behavior of
aborting on unhandled exceptions even on background threads is much
cleaner than the previous behavior in 1.x.

Best regards,
Martin
May 19 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
8
by: Mahesh Devjibhai Dhola [MVP] | last post by:
We are building Chat like application using Forms and as a result our programming is becoming complicated to display messages received on different threads in the chat window (due to STA...
10
by: Danny Carvajal | last post by:
Hello, I have an application that uses 5 threads. So I simply create the 5 threads. However, I need to be able to create x number of threads at a time. Not sure how to do this. Any links to...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
17
by: Arun Kumar | last post by:
What is wrong with this code. All i am trying to test is 3 progressbar and one button. On buttonclick i create 3 threads and each thread calls a method which in turn updates the progressbar and it...
10
by: Roberto López | last post by:
Hi, I´m doing an asp.net application that uploads and downloads files and folders between the client and the server on my intranet. To do this I have create threads and it runs Ok but I need to...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.