c# lambda/closure executes when #again

The increment below is not executed when CLR executes Line #2 which sets up the thread. It’s executed only when the thread gets the “driver’s seat“, which doesn’t happen when Line #2 executes.

int x = 0; //#1
ThreadPool.QueueUserWorkItem(() => x++); //#2

The void lambda is more like a standalone method. This method is not executed when it seeds the new thread. It’s executed only when the thread runs.

More specifically, this lambda is more like a nameless closure —

Action myClosure = (() => x++);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s