473,586 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to lock other threads


Hi all.

I wrote this C# code:

lock(this)
{

/ /write to a file

}

thinking that it was going to make other threads wait in line to access
the same block of code. But as I now understand it this only locks the
current thread.

Does anyone know if there a way to do what I was originally intending?
- stop other threads from simultaneously accessing the code inside a
given block?

Thanks,

Jeff

Jan 24 '07 #1
5 3375
Hi,

<je*********@gm ail.comwrote in message
news:11******** **************@ q2g2000cwa.goog legroups.com...
|
| Hi all.
|
| I wrote this C# code:
|
| lock(this)
| {
|
| / /write to a file
|
| }
|
|
|
| thinking that it was going to make other threads wait in line to access
| the same block of code. But as I now understand it this only locks the
| current thread.

What make you think that?
As long as the other threads lock on the same instance only one thread will
have access to writing the file.

Jan 24 '07 #2
This is not true. And if you think about it it deosn't make any sense. Any
thread that tries to execute the lock will block if the object has been
locked already by another thread. If a thread makes an attempt to lock an
object that it already lock it just goes through the lock - no thread can
lock itself.

Locking is a technique for interthread synchronization , thus what you said
cannot be true.
--
Stoitcho Goutsev (100)

<je*********@gm ail.comwrote in message
news:11******** **************@ q2g2000cwa.goog legroups.com...
>
Hi all.

I wrote this C# code:

lock(this)
{

/ /write to a file

}

thinking that it was going to make other threads wait in line to access
the same block of code. But as I now understand it this only locks the
current thread.

Does anyone know if there a way to do what I was originally intending?
- stop other threads from simultaneously accessing the code inside a
given block?

Thanks,

Jeff

Jan 24 '07 #3
Jeff,

Why don't you create a member which you can use to lock? You could use
Interlocked class, ReadWriterLock class, Monitor class, etc, to manipulate
this object. Even using just lock would work.

Best,

--
Regards,
Robson Siqueira
Enterprise Architect
<je*********@gm ail.comwrote in message
news:11******** **************@ q2g2000cwa.goog legroups.com...
>
Hi all.

I wrote this C# code:

lock(this)
{

/ /write to a file

}

thinking that it was going to make other threads wait in line to access
the same block of code. But as I now understand it this only locks the
current thread.

Does anyone know if there a way to do what I was originally intending?
- stop other threads from simultaneously accessing the code inside a
given block?

Thanks,

Jeff

Jan 24 '07 #4


Thanks to everyone for your helpful info.

You guys are right that my statement 'only locks the current thread'
makes no sense. I was going to add that as part of my question.. the
fact that i didn't understand even the meaning of that. 'locking'
*means* locking for other threads..., you are right.

Someone at work told me this. But thinking about it what I think she
told me was that this lock statement was in an object that was not a
Singleton.. different threads were making their own instance of of
this object, and that therefore the lock was basically doing nothing.
Because it would only block other threads attempting to get inside that
code from the same instance of that object in which the lock statement
resides.

Is she right and if so, how can I lock the other threads?

Thanks again!!

Jeff


On Jan 24, 1:37 pm, "Robson Siqueira" <rob...@robsonf elix.comwrote:
Jeff,

Why don't you create a member which you can use to lock? You could use
Interlocked class, ReadWriterLock class, Monitor class, etc, to manipulate
this object. Even using just lock would work.

Best,

--
Regards,
Robson Siqueira
Enterprise Architect<jeff. ran...@gmail.co mwrote in messagenews:11* *************** ******@q2g2000c wa.googlegroups .com...


Hi all.
I wrote this C# code:
lock(this)
{
/ /write to a file
}
thinking that it was going to make other threads wait in line to access
the same block of code. But as I now understand it this only locks the
current thread.
Does anyone know if there a way to do what I was originally intending?
- stop other threads from simultaneously accessing the code inside a
given block?
Thanks,
Jeff- Hide quoted text -- Show quoted text -
Jan 26 '07 #5
Jeff,

She is right that if you want the lock to work all the thread should lock on
the same instance of the object. Otherwise each thread will acquire lock on
different objects thus there won't be any interthread locking and the code
will run as there is no lock at all.

--
HTH
Stoitcho Goutsev (100)
<je*********@gm ail.comwrote in message
news:11******** **************@ h3g2000cwc.goog legroups.com...
>

Thanks to everyone for your helpful info.

You guys are right that my statement 'only locks the current thread'
makes no sense. I was going to add that as part of my question.. the
fact that i didn't understand even the meaning of that. 'locking'
*means* locking for other threads..., you are right.

Someone at work told me this. But thinking about it what I think she
told me was that this lock statement was in an object that was not a
Singleton.. different threads were making their own instance of of
this object, and that therefore the lock was basically doing nothing.
Because it would only block other threads attempting to get inside that
code from the same instance of that object in which the lock statement
resides.

Is she right and if so, how can I lock the other threads?

Thanks again!!

Jeff


On Jan 24, 1:37 pm, "Robson Siqueira" <rob...@robsonf elix.comwrote:
>Jeff,

Why don't you create a member which you can use to lock? You could use
Interlocked class, ReadWriterLock class, Monitor class, etc, to
manipulate
this object. Even using just lock would work.

Best,

--
Regards,
Robson Siqueira
Enterprise Architect<jeff. ran...@gmail.co mwrote in
messagenews:11 *************** *******@q2g2000 cwa.googlegroup s.com...


Hi all.
I wrote this C# code:
lock(this)
{
/ /write to a file
}
thinking that it was going to make other threads wait in line to access
the same block of code. But as I now understand it this only locks the
current thread.
Does anyone know if there a way to do what I was originally intending?
- stop other threads from simultaneously accessing the code inside a
given block?
Thanks,
Jeff- Hide quoted text -- Show quoted text -

Jan 26 '07 #6

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

Similar topics

0
4525
by: John | last post by:
I've got multiple threads and processes that write to same file. Before writing all threads / processes first lock part of file and then write to file. If one thread / process locks file, another threads / processes can not lock this file again. And I want that all threads / processes wait until first thread frees lock by calling...
7
703
by: Sunny | last post by:
Hi, I can not understend completely the lock statement. Actally what is locked: 1. the part of the code between {...} or 2. the object in lock() In the docs is written: for 1: The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a
0
17759
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico Mariani, performance architect for the Microsoft® .NET runtime and longtime Microsoft developer, mentioned to Dr. GUI in an e-mail conversation...
5
29542
by: Seeker | last post by:
Hello, I've read conflicting posts about . Does it or does it not lock the entire object? In my simple test it appears to block just the method but I wouldn't exactly call my meager test conclusive... thanks, Scott
1
4383
by: Jesper | last post by:
Hi, Once I wrote a singlethreaded program containing a somewhat large tree structured set of objects. A treeview was assigned this structure and it was possible to drag and drop objects - i.e. reorganize the tree structure. Each object, tagged to a tree node, had a ref to its own userform, however, after a drag drop operation a...
0
1273
by: Alvin A. Delagon | last post by:
I have a multithreaded application that spawns threads which query a database server. During stress test I encountered some threads failing due "lost connection errors" and sometimes the actual script itself dies due to a "Segmentation Fault". I realized that it might be a deadlock situation so I applied a lock mechanism but after another...
5
4057
by: Bob Bins | last post by:
Is there a way to create a shared lock using the Monitor class or any other synchronization class in .NET? So I have one resource that may have multiple threads using it at once but a second thread that when called must have exclusive access and cause the other threads to wait. I can't figure out how to do this with .Net. Thanks.
20
1935
by: Kurt | last post by:
Below is a class that can accessed from multiple threads and I want the class to be thread safe. I have a private timer member whose interval can be changed by different threads. Which is the correct way to define the property below. Thanks Kurt Class1 { private Timer _timer = new Timer(); // Example 1
94
30252
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8215
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.