473,403 Members | 2,270 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,403 software developers and data experts.

Thread class - using lock object

Hi ,

I had a doubt regarding a piece of code with me , that has to do with
System.Threading.Thread class .

In it user instantiates an array of Thread class and to all of them
assign a method for execution using ThreadStart Delegate say "worker"
method .

Now in the worker method there's a piece of code :

lock (typeof(<ClassName>))
{
try
{
Thread.CurrentThread.Name = AppDomain.GetCurrentThreadId().ToString();
}
catch (System.Exception ex)
{
// Exception code
}
}

where as you can see there's a Synchronized region been created using
lock keyword .

However , what i think is , this is not required since , Worker method
is an instance method and every thread will execute it's own separate
method , so , there's no need , but then another point is all this code
is as a part of an exe , which has only one instance created at any
point of time , so it seems may be required even .

Any comments regarding this ?

thanks ,

Mrinal
Nov 21 '05 #1
4 2131
Mrinal,

For the code that you listed, you don't need the lock at all.

You are looking to assign to the value of the current thread, which will
be different for every thread that this routine is called on. Since that is
the case, you don't have to worry about multiple threads accessing the
value.

It should also be noted that you shouldn't use the static
GetCurrentThreadId method on the AppDomain class anymore, since it is not
guaranteed to give you an id which corresponds to the current thread.
Threads in .NET are no longer mapped to physical threads on the machine.
They are logical entities and should be treated as such.

In general, when you are working with threads, you need to use a lock
statement to synchronize access between threads when they share a common
resource. Since you are not sharing anything here, it's a non-issue.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mrinal Kamboj" <mr***********@oracle.com> wrote in message
news:6N**************@news.oracle.com...
Hi ,

I had a doubt regarding a piece of code with me , that has to do with
System.Threading.Thread class .

In it user instantiates an array of Thread class and to all of them assign
a method for execution using ThreadStart Delegate say "worker" method .

Now in the worker method there's a piece of code :

lock (typeof(<ClassName>))
{
try
{
Thread.CurrentThread.Name = AppDomain.GetCurrentThreadId().ToString();
}
catch (System.Exception ex)
{
// Exception code
}
}

where as you can see there's a Synchronized region been created using lock
keyword .

However , what i think is , this is not required since , Worker method is
an instance method and every thread will execute it's own separate method
, so , there's no need , but then another point is all this code is as a
part of an exe , which has only one instance created at any point of time
, so it seems may be required even .

Any comments regarding this ?

thanks ,

Mrinal

Nov 21 '05 #2
Nicholas ,

Thanks again , but few things , i agree Thread class in .Net is a
logical wrapper , but then underlying unmanaged implementation should
correspond to OS threads , essentially , what i mean is on doing :

Thread t = new Thread(<Threadstart delegate>) ;

it should spawn a corresponding entity in the OS and on using Appdomain
static method , i should be able to get the relevant id . What are your
views regarding this .

also , what if i use :

[DllImport("kernel32.dll", EntryPoint="GetCurrentThreadId")]
public static extern uint GetCurrentThreadId();

so , it will be :

Thread.CurrentThread.Name = GetCurrentThreadId().ToString() ;

instead of Appdomain's static method , is it doing the same thing .

regards ,

Mrinal
Nicholas Paldino [.NET/C# MVP] wrote:
Mrinal,

For the code that you listed, you don't need the lock at all.

You are looking to assign to the value of the current thread, which will
be different for every thread that this routine is called on. Since that is
the case, you don't have to worry about multiple threads accessing the
value.

It should also be noted that you shouldn't use the static
GetCurrentThreadId method on the AppDomain class anymore, since it is not
guaranteed to give you an id which corresponds to the current thread.
Threads in .NET are no longer mapped to physical threads on the machine.
They are logical entities and should be treated as such.

In general, when you are working with threads, you need to use a lock
statement to synchronize access between threads when they share a common
resource. Since you are not sharing anything here, it's a non-issue.

Hope this helps.

Nov 21 '05 #3
NO. You CAN NOT do this in .NET 2.0. It is not guaranteed that there
will be a one-to-one relationship between a thread id and a managed thread
in .NET.

In .NET 1.1 and before, you could do this, but it wouldn't work in .NET
2.0 and beyond, since multiple Threads can actually execute on the same OS
thread.

If you need a unique identifier for a thread, then use the
ManagedThreadId property on the Thread instance.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mrinal Kamboj" <mr***********@oracle.com> wrote in message
news:43**************@oracle.com...
Nicholas ,

Thanks again , but few things , i agree Thread class in .Net is a logical
wrapper , but then underlying unmanaged implementation should correspond
to OS threads , essentially , what i mean is on doing :

Thread t = new Thread(<Threadstart delegate>) ;

it should spawn a corresponding entity in the OS and on using Appdomain
static method , i should be able to get the relevant id . What are your
views regarding this .

also , what if i use :

[DllImport("kernel32.dll", EntryPoint="GetCurrentThreadId")]
public static extern uint GetCurrentThreadId();

so , it will be :

Thread.CurrentThread.Name = GetCurrentThreadId().ToString() ;

instead of Appdomain's static method , is it doing the same thing .

regards ,

Mrinal
Nicholas Paldino [.NET/C# MVP] wrote:
Mrinal,

For the code that you listed, you don't need the lock at all.

You are looking to assign to the value of the current thread, which
will be different for every thread that this routine is called on. Since
that is the case, you don't have to worry about multiple threads
accessing the value.

It should also be noted that you shouldn't use the static
GetCurrentThreadId method on the AppDomain class anymore, since it is not
guaranteed to give you an id which corresponds to the current thread.
Threads in .NET are no longer mapped to physical threads on the machine.
They are logical entities and should be treated as such.

In general, when you are working with threads, you need to use a lock
statement to synchronize access between threads when they share a common
resource. Since you are not sharing anything here, it's a non-issue.

Hope this helps.


Nov 21 '05 #4
Hi Mrinal,

Thanks for Nicholas's reply. I just wanted to check how things are going
and whether or not Nicholas's suggestion solve your problem.

If there is any question, please feel free to join the community and we are
here to support you at your convenience. Thanks again and have a nice day!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 24 '05 #5

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

Similar topics

31
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
0
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
7
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
8
by: Senna | last post by:
Hi I have some questions about this code, http://code.doria.se/?c=18. 1. Will the Adapter class be thread-safe? 2. In the #region Way 1 I use lock(_lockobj) every time I go about and do...
2
by: Gabriele Farina | last post by:
Hi, I'm tring to implement a Singleton object that should be specific for every thread who create it, not global. I tried a solution that seems to work, but I have a very poor knowledge of...
5
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
12
by: Ronny | last post by:
Thanks Chris, Looks nice but I miss the dual way communication. In the main thread to deliver paramters and data to the worker thread- how can I do that? Regards Ronny Take a look at the...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.