473,508 Members | 2,805 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 3366
Hi,

<je*********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.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*********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.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*********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.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...@robsonfelix.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.comwrote in messagenews:11**********************@q2g2000cwa.go oglegroups.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*********@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.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...@robsonfelix.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.comwrote in
messagenews:11**********************@q2g2000cwa.g ooglegroups.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
4501
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...
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...
0
17731
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...
5
29486
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...
1
4371
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....
0
1268
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...
5
4048
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...
20
1921
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...
94
30187
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...
0
7129
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
7333
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
7398
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
7502
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...
0
5637
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,...
1
5057
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...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
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 ...
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.