473,382 Members | 1,409 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,382 software developers and data experts.

Help needed with simple multi-threading sample code

The following sample code for the lock statement is on page 112 of the
O'Reilly book "C# Essentials". Can somebody explain to me why this
recursive class definition of LockTest does not cause an infinite number of
LockTest objects to be created (i.e., until it consumes all available
memory)? I don't understand why only two threads are created.
using System;
using System.Threading;

class LockTest
{
static void Main()
{
LockTest lt = new LockTest();
Thread t = new Thread(new ThreadStart(lt.Go));
t.Start();
lt.Go();
}
void Go()
{
lock(this)
for (char c='a'; c<='z'; c++)
Console.Write(c);
}
}
Nov 16 '05 #1
2 1240
Well, lock is used for a critical section, so if a second thread tries to access the code in the locked section, it will be forced to wait until the locking thread has finished. So, with that in mind, the first thread is created, then the secodn which waits on the first, then the first finished, and the seond thread gets the lock, then a new thread is created, and waits on that thread, and so on.....

Does that help?

Martin.

"Mark Huebner" wrote:
The following sample code for the lock statement is on page 112 of the
O'Reilly book "C# Essentials". Can somebody explain to me why this
recursive class definition of LockTest does not cause an infinite number of
LockTest objects to be created (i.e., until it consumes all available
memory)? I don't understand why only two threads are created.
using System;
using System.Threading;

class LockTest
{
static void Main()
{
LockTest lt = new LockTest();
Thread t = new Thread(new ThreadStart(lt.Go));
t.Start();
lt.Go();
}
void Go()
{
lock(this)
for (char c='a'; c<='z'; c++)
Console.Write(c);
}
}

Nov 16 '05 #2
Hi Mark
Because there is no recursive call in that, all what is done here is that
you are just creating an instance of an object in the main method of the
class. Because main is the entry point , it is run at the start of the
program AND IT RUN ONLY ONCE . Notice that the main is static function i.e.
it is a function of the class not of the class objects. What you are
expecting would happen if there were a call to main from within the main
function, which is not there.
In fact this is the model that is used in c# windows application. Where the
main from is created from within the main function of its class.
Hope that was clear

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #3

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

Similar topics

5
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need...
2
by: Chris M. Gamble | last post by:
I am trying to perform what I best understand as Multi-master asynchronous replication for postgres 7.3.3 servers. After researching, I tried the pgReplication project (and made the simple...
1
by: Dominic Grosleau | last post by:
When i tried to compile this test driver for an actual conversion library i'm trying to make I got the following messages from gcc. /*problems reported*/ gcc convertlib.c main.c -ansi...
4
by: Random | last post by:
The way I've built my page is to take the user through a multi-form process, only rendering those controls that are needed for each section. The ViewState is working the way I want it to,...
3
by: Joe Befumo | last post by:
This is my first attempt at multi-thread programming, and I'm encountering a program-logic problem that I can't quite put my finger on. The program is pretty simple. I'm trying to validate a...
5
by: Olly | last post by:
Hello Everyone! Could someone please have a look at my JS Form I posted below....Something wrong there, but I don't understand what's exactly. Many thanks. Olly ...
3
by: Ron | last post by:
Hi All, Okay, here's the deal: Access2000/WinXP. Have a database that's split, FE/BE with multiple users having FE and one of those users also has BE (still split though...). Have a form on...
1
by: Skord | last post by:
Hi, I have to develop a piece of software for the company that I work. It's a very specialised piece of software and will be quite a complicated database. It will be a Microsoft SQL Server 2005...
0
by: boblatest | last post by:
Hello, please accept my apologies for the OT posting. I've tried to get an answer over at www.authoring.misc but didn't. So I'm hoping that I'll get some input from this group from which I've...
19
by: uhdam | last post by:
Hai... I need to have image as tooltip. Is it possible. If yes how. I dont want to use applet. When i scroll over the label in my frame the pop up containing the image should be displayed. How can...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.