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

Threading/Locking

I have a question about locks because I am seeing behavior that does not
match what I though was the correct behavior. I thought when you put a lock
around code the first instance that ran it would of course get access and
then each access after that (while the first instance is still executing the
code) would be essentially queued up on that lock waiting for the first to
exit. But what I am seeing is that the last one there is the first to get
access to the code... which doesn't make sense. If someone can shed some
light I would appreciate it. Below is an example of the code and the data
that it returned you can see that 4 lines that were logged before the lock
and the last one was the first to go inside the lock????

5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ======= DATE
5/11/2005 11:26:43 AM - Data After Lock: ======= DATE
5/11/2005 11:26:43 AM - Data Before Lock: of BIRTH: 12/
5/11/2005 11:26:43 AM - Data After Lock: of BIRTH: 12/
5/11/2005 11:26:43 AM - Data After Lock: ==============
5/11/2005 11:26:43 AM - Data After Lock: ==============
5/11/2005 11:26:43 AM - Data After Lock: ==============
//lockingObject is a private object contained in the class with this method
public void getIncomingData(object data)
{
try
{
myLog.Log(string.Format("Data Before Lock: {0}",data),true);
lock(lockingObject)
{
myLog.Log(string.Format("Data After Lock: {0}",data),true);
myParser.getIncomingData((string)data);
}
}
catch(Exception e)
{
myLog.Log(string.Format("Error in DataCapture getIncomingData:
{0}",e.Message));
}
}

Nov 17 '05 #1
9 1198
Shane <Sh***@discussions.microsoft.com> wrote:
I have a question about locks because I am seeing behavior that does not
match what I though was the correct behavior. I thought when you put a lock
around code the first instance that ran it would of course get access and
then each access after that (while the first instance is still executing the
code) would be essentially queued up on that lock waiting for the first to
exit. But what I am seeing is that the last one there is the first to get
access to the code... which doesn't make sense. If someone can shed some
light I would appreciate it. Below is an example of the code and the data
that it returned you can see that 4 lines that were logged before the lock
and the last one was the first to go inside the lock????


There's no guaranteed ordering involving in who acquires the lock when.
I don't believe it's specified to be FIFO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
So what is the suggested solution to this...I need the data to be handled in
order, but I don't want to have to block the sending thread because the data
coming in is time sensitive (ie needs to be retrieved quickly).

thanks for your help.

"Jon Skeet [C# MVP]" wrote:
Shane <Sh***@discussions.microsoft.com> wrote:
I have a question about locks because I am seeing behavior that does not
match what I though was the correct behavior. I thought when you put a lock
around code the first instance that ran it would of course get access and
then each access after that (while the first instance is still executing the
code) would be essentially queued up on that lock waiting for the first to
exit. But what I am seeing is that the last one there is the first to get
access to the code... which doesn't make sense. If someone can shed some
light I would appreciate it. Below is an example of the code and the data
that it returned you can see that 4 lines that were logged before the lock
and the last one was the first to go inside the lock????


There's no guaranteed ordering involving in who acquires the lock when.
I don't believe it's specified to be FIFO.

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

Nov 17 '05 #3
"Shane" <Sh***@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
I have a question about locks because I am seeing behavior that does not
match what I though was the correct behavior. I thought when you put a
lock
around code the first instance that ran it would of course get access and
then each access after that (while the first instance is still executing
the
code) would be essentially queued up on that lock waiting for the first to
exit. But what I am seeing is that the last one there is the first to get
access to the code... which doesn't make sense. If someone can shed some
light I would appreciate it. Below is an example of the code and the data
that it returned you can see that 4 lines that were logged before the lock
and the last one was the first to go inside the lock????


I was wondering, are you sure there is not already a lock on lockingobject
before that first try?

-- Alan
Nov 17 '05 #4
Those log messages were not the first calls they were further down the list
there had already been locks put on that object, but that is the only place
that I am using that object.
Nov 17 '05 #5
"Shane" <Sh***@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
Those log messages were not the first calls they were further down the
list
there had already been locks put on that object, but that is the only
place
that I am using that object.


One thing you can do is give unique names to all of the threads you create
and put a breakpoint at/after the lock and see who is getting in. You can
also freeze threads in the debugger while concentrating on the one of
interest.

-- Alan
Nov 17 '05 #6
Shane <Sh***@discussions.microsoft.com> wrote:
So what is the suggested solution to this...I need the data to be
handled in order, but I don't want to have to block the sending
thread because the data coming in is time sensitive (ie needs to be
retrieved quickly).


Then you stash the data in an ordered list which is then processed by
another thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
I know what thread is calling the function thats not really the problem its
the behavior. I need a way to make sure that the threads are executed in the
order that they were spun off. Or at least to work on the data in the order
that it comes in.

Thanks for your help

"Alan Pretre" wrote:
"Shane" <Sh***@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
Those log messages were not the first calls they were further down the
list
there had already been locks put on that object, but that is the only
place
that I am using that object.


One thing you can do is give unique names to all of the threads you create
and put a breakpoint at/after the lock and see who is getting in. You can
also freeze threads in the debugger while concentrating on the one of
interest.

-- Alan

Nov 17 '05 #8
"Shane" <Sh***@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
I know what thread is calling the function thats not really the problem its
the behavior. I need a way to make sure that the threads are executed in
the
order that they were spun off. Or at least to work on the data in the
order
that it comes in.


Yes I think that is a problem. You can't really control the timeslices that
the threads receive and so there really is no guarantee on who will arrive
at the gate first. If there is one point of arrival it would be better to
have only one thread fetching. You can have other threads doing the
processing from the collection.

You can use a System.Collections.Queue object and can coordinate the storing
and retrieving on the queue through its SyncRoot member. It is optimized
for multithreaded access to the collection. This would eliminate your
lockingobject.

-- Alan
Nov 17 '05 #9
Thanks for your help Alan...

Thats what I ended up doing putting the data into a Queue and then creating
a thread and then using the SyncRoot of the Queue to lock it so that the data
was read out of the queue in the order it came in.

Thanks all.

"Alan Pretre" wrote:
"Shane" <Sh***@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
I know what thread is calling the function thats not really the problem its
the behavior. I need a way to make sure that the threads are executed in
the
order that they were spun off. Or at least to work on the data in the
order
that it comes in.


Yes I think that is a problem. You can't really control the timeslices that
the threads receive and so there really is no guarantee on who will arrive
at the gate first. If there is one point of arrival it would be better to
have only one thread fetching. You can have other threads doing the
processing from the collection.

You can use a System.Collections.Queue object and can coordinate the storing
and retrieving on the queue through its SyncRoot member. It is optimized
for multithreaded access to the collection. This would eliminate your
lockingobject.

-- Alan

Nov 17 '05 #10

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

Similar topics

2
by: Fuzzyman | last post by:
Looking at threading code it seems that the Global Interpreter Lock is a bit of a 'brute force' way of preventing two threads attempting to access sensitive objects simultaneously.... Why not...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
10
by: muscha | last post by:
Hi All, I'm having a bit of problem with threading, when I first call Start() on my code the thread work, I call Stop() and it stopped, but when I call Start() again my thread is not started....
1
by: Tom | last post by:
I've googled, and read, and stripped out code, and rewritten code, and still can't get a System.Threading.Timer to work. (I hereby publicly admit that I'm a failure here...) Could someone please...
3
by: Asad | last post by:
Hi, I am trying to write some threading code to my application. The reason I've been tempted to do this is because, I am doing some FTP uploads, and sometimes during the put method, the...
10
by: jt | last post by:
The program works like this: There is a form with a button. When the form is loaded, a separate thread is started which is retreiving/updating data in the database every x seconds. When clicked...
4
by: DBC User | last post by:
I have a background process which reads a table to see if there are any pending requests. If there are any, then it will start a worker thread (only 10 allowed at a time) and executes a method. In...
14
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I just started learning Threading in C#. I read lot of articles and what I am trying to accomplish here is that I have an array of string and I want to pass the member of that...
9
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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,...
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,...

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.