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

Threads in C#

Hey,

Having a few problems using threads in C#. In the msdn documentation,
mutual exclusion is only discussed when dealing with threads that all
execute the same method.

What's the actual meaning of the lock function?

All I want to do is keep other threads from entering their critical
sections when one of the threads is its critical section.

The critical sections are different though, they are not located inside
the same method.

Any help please?

Thanks in advance,

Luk Vloemans

Nov 15 '05 #1
2 1452
Luk,

lock can be applied to any reference object (i.e. to objects of classes, not
of structs). The simplest way to use it is usually lock (this).

class X
{
func a ()
{
lock (this)
{
// do stuff
}
}

func b ()
{
lock (this)
{
// do stuff
}
}
}

Since both a and b lock on the same things, then one thread entering a
excludes others from entering b (as well as a). Note that this is per object
locking. Do do per class locking you'd have to lock on a static member.

More refined locking can be done by locking on a specific object:

class X
{
string aData;
string bData;

func a ()
{
lock (aData)
{
// manipulate aData
}
}

func b ()
{
lock (bData)
{
// manipulate bData
}
}
}

If you one broader locking, then you just need to share some object between
all the objects involved and lock on that in your various methods:

Regards,

Jasper Kent.

"Luk Vloemans" <lu**********@student.luc.ac.be> wrote in message
news:Op**************@TK2MSFTNGP10.phx.gbl...
Hey,

Having a few problems using threads in C#. In the msdn documentation,
mutual exclusion is only discussed when dealing with threads that all
execute the same method.

What's the actual meaning of the lock function?

All I want to do is keep other threads from entering their critical
sections when one of the threads is its critical section.

The critical sections are different though, they are not located inside
the same method.

Any help please?

Thanks in advance,

Luk Vloemans

Nov 15 '05 #2
Jasper Kent <ja*********@hotmail.com> wrote:
lock can be applied to any reference object (i.e. to objects of classes, not
of structs). The simplest way to use it is usually lock (this).


I would strongly recommend *not* locking on this. Instead, lock on a
private reference which *only* the class itself can see; you can also
have various locks for if you want to lock in different circumstances
(for instance, lock all method calls to A against each other, and all
method calls to B against each other, but you can have one of each of A
and B running at the same time). This way you can make the lock
variable name descriptive of *exactly* its purpose, and because no
other code will have a reference to what you're locking, they can't end
up locking on the same thing and causing problems.

In the rare occasions where you need multiple classes to lock on the
same reference, again I'd suggest creating a separate object just for
the purpose of locking.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3

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

Similar topics

3
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import...
0
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux...
10
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes,...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
10
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that...
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?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.