473,398 Members | 2,427 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,398 software developers and data experts.

Locking threads

Hi

I have a server listening and when it receives an object deserilalises it.
Reads it, does whatever it needs, reserialises it and then broadcasts it to
clients (depending on what object was sent, found when it deseriliased it).

The problem is this:

1) Server receives object, deserialises
2) reads and serilaises again
3) go into broadcast loop to all clients. <-------but while doing this
receives a new object and deserialises it!

My comment in point 3 i believe is what is occurring. How can i protect such
a situation. This is what i believe is the solution but i would like someone
to either confirm or point me the right way if i am wrong.

i do this on the broadcast now:

lock(staticObject)
{
DoBroadCast(objToSend);
}

My thinking is that by doing that:

1) it locks any other thread from altering my objToSend object until
DoBroadcast is done with it.
2) All new objects sent to server will wait until they can access the obj so
they can deseriliase into it.
3) Broadcast finishes, releases lock, andnext object can now be deserialised
and used by server.

Am i right in my logic of how it will react to that lock?

Thanks
Feb 13 '07 #1
3 1564

Don't store the object as an instance field. Deserialize it to a
local variable and pass it along as a parameter to the other functions
(deserialize, process, rebroardcast, etc.)

You don't want to add extra locking to force your system to only
handle one object at a time 'cause that would be very poor concurrency
and could cause a backlog of data to be processed.

You may also want to look into using multiple threads for the
broadcast to other clients.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Tue, 13 Feb 2007 04:20:03 -0000, "PokerMan" <no****@pokercat.co.uk>
wrote:
>Hi

I have a server listening and when it receives an object deserilalises it.
Reads it, does whatever it needs, reserialises it and then broadcasts it to
clients (depending on what object was sent, found when it deseriliased it).

The problem is this:

1) Server receives object, deserialises
2) reads and serilaises again
3) go into broadcast loop to all clients. <-------but while doing this
receives a new object and deserialises it!

My comment in point 3 i believe is what is occurring. How can i protect such
a situation. This is what i believe is the solution but i would like someone
to either confirm or point me the right way if i am wrong.

i do this on the broadcast now:

lock(staticObject)
{
DoBroadCast(objToSend);
}

My thinking is that by doing that:

1) it locks any other thread from altering my objToSend object until
DoBroadcast is done with it.
2) All new objects sent to server will wait until they can access the obj so
they can deseriliase into it.
3) Broadcast finishes, releases lock, andnext object can now be deserialised
and used by server.

Am i right in my logic of how it will react to that lock?

Thanks
Feb 13 '07 #2
Hi Sam

Thanks for replying, but firstly in answer to my post, regardless if its the
best way, i am aware of performance hit, is my thinking as to the way the
lock would effect my programme right?

I do serialiase to a local object. The problem is that my asynchronous
action that fills a data stream from the data received has to be a instance
field. It is this that i believe is getting changed before my handling code
can complete. It passes that var into the handling code, that then
deserialises to a local object.

So do you think my best move is to do my broadcast on a new thread and lock
that thread? To be honest i am not sure why my object seems to be getting
changed during a broadcast, or during a serilsation. My code does seem to
handle it, however due to the issues and the 'symptoms' all pointing to that
problem i felt locks were needed. If that solves it then i know for sure
this is the cause. Then i will go through removing locks and looking at
better faster solutions to the same issue. (For example i can serialise the
data once, then send to all clients, at present i serilalise and send
everytime ;) ).

Look forward to all suggestiongs or thoughts. Importantly i'd like to know
if my thinking on original post was right, since no disagreement i will
assume i am right?
"Samuel R. Neff" <sa********@nomail.comwrote in message
news:1r********************************@4ax.com...
>
Don't store the object as an instance field. Deserialize it to a
local variable and pass it along as a parameter to the other functions
(deserialize, process, rebroardcast, etc.)

You don't want to add extra locking to force your system to only
handle one object at a time 'cause that would be very poor concurrency
and could cause a backlog of data to be processed.

You may also want to look into using multiple threads for the
broadcast to other clients.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Tue, 13 Feb 2007 04:20:03 -0000, "PokerMan" <no****@pokercat.co.uk>
wrote:
>>Hi

I have a server listening and when it receives an object deserilalises it.
Reads it, does whatever it needs, reserialises it and then broadcasts it
to
clients (depending on what object was sent, found when it deseriliased
it).

The problem is this:

1) Server receives object, deserialises
2) reads and serilaises again
3) go into broadcast loop to all clients. <-------but while doing this
receives a new object and deserialises it!

My comment in point 3 i believe is what is occurring. How can i protect
such
a situation. This is what i believe is the solution but i would like
someone
to either confirm or point me the right way if i am wrong.

i do this on the broadcast now:

lock(staticObject)
{
DoBroadCast(objToSend);
}

My thinking is that by doing that:

1) it locks any other thread from altering my objToSend object until
DoBroadcast is done with it.
2) All new objects sent to server will wait until they can access the obj
so
they can deseriliase into it.
3) Broadcast finishes, releases lock, andnext object can now be
deserialised
and used by server.

Am i right in my logic of how it will react to that lock?

Thanks

Feb 13 '07 #3
PokerMan <no****@pokercat.co.ukwrote:
So do you think my best move is to do my broadcast on a new thread and lock
that thread? To be honest i am not sure why my object seems to be getting
changed during a broadcast, or during a serilsation. My code does seem to
handle it, however due to the issues and the 'symptoms' all pointing to that
problem i felt locks were needed.
Thread safety is a delicate matter, and can't be properly solved by
adding in locks until everything appears to work.

I think you have some mistaken beliefs about what locking does, given
this post and your original one. You don't lock a thread - you acquire
a lock on the monitor associated with an object. That won't stop
anything else from changing the contents of that object - it will only
stop other threads from acquiring the same monitor.

Threading is too big a topic to go into much detail on in a newsgroup
post, but you might want to have a look at my threading article for
more discussion:

http://pobox.com/~skeet/csharp/threads

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '07 #4

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

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
4
by: Michael Chermside | last post by:
Ype writes: > For the namespaces in Jython this 'Python internal thread safety' > is handled by the Java class: > > http://www.jython.org/docs/javadoc/org/python/core/PyStringMap.html > > which...
12
by: Alban Hertroys | last post by:
Good day, I have a number of threads doing inserts in a table, after which I want to do a select. This means that it will occur that the row that I want to select is locked (by the DB). In these...
3
by: Morten Wennevik | last post by:
Hi, I have a MDIChild with static properties that can be updated at any time by the MDIParent. These properties may also at unknown times be accessed by one or more worker threads. Now, I'm...
16
by: akantrowitz | last post by:
In csharp, what is the correct locking around reading and writing into a hashtable. Note that the reader is not looping through the keys, simply reading an item out with a specific key: If i...
7
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets...
6
by: shaanxxx | last post by:
I have global variable which is being shared between threads (problem is not connected with thread). Without using any mutex i have do some operation global variable in *consistent* way. ...
5
by: Chris Mullins | last post by:
I've spent some time recently looking into optimizing some memory usage in our products. Much of this was doing through the use of string Interning. I spent the time and checked numbers in both x86...
4
by: Mark S. | last post by:
Much to my surprised the code below compiled and ran. I just don't know enough about threading to know for sure if this is too good to be true. I'm attempting to isolate the Hashtable lock to...
15
by: Matt Brandt | last post by:
I am trying to get multiple threads to lock specific regions of a file. However, since each thread has the same PID, it appears that a lock by one thread does not block another thread from the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.