471,594 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

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 3246
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Sunny | last post: by
reply views Thread by Nashat Wanly | last post: by
reply views Thread by Alvin A. Delagon | last post: by
5 posts views Thread by Bob Bins | last post: by
20 posts views Thread by Kurt | last post: by
94 posts views Thread by Samuel R. Neff | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.