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

Syncronization question

Hi,

Hope this question makes sense. =)

I have an object, and a wrapper for that object that implements a
synchronized view (similar to getting a synchronized ArrayList).

What I'm wondering is, if I need to lock all methods? Here's an example:

I have 3 versions of an IndexOf method:

public override void IndexOf(object value) {
return IndexOf(object value, 0)
}

public override void IndexOf(object value, int startIndex) {
return IndexOf(object value, 0, __innerObject.__count);
}

public override void IndexOf(object value, int startIndex, int count) {
lock (__innerObject) {
return (__innerObject.IndexOf(value, startIndex, count);
}
}

is it possible, if i don't lock all the IndexOf methods, that i could get
unexpected results? Or will I be fine since I end up calling a method that
is locked?

Thanks,
Curtis
Nov 15 '05 #1
2 1491
Hi,
It is possible to get unexpected results, because access to
__innerObject.__count instance variable isn't serialized. If another thread
changed value of __innerObject.__count before the first thread redirects the
call to the synchronized method, the count parameter will have invalid
value.
--
Andrew Gnenny
pulsar2003@/no-spam/email.ru (Please remove /no-spam/ for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
"Curtis Hatter" <cu**********@fuse.net> wrote in message
news:3f***********************@nnrp.fuse.net...
Hi,

Hope this question makes sense. =)

I have an object, and a wrapper for that object that implements a
synchronized view (similar to getting a synchronized ArrayList).

What I'm wondering is, if I need to lock all methods? Here's an example:

I have 3 versions of an IndexOf method:

public override void IndexOf(object value) {
return IndexOf(object value, 0)
}

public override void IndexOf(object value, int startIndex) {
return IndexOf(object value, 0, __innerObject.__count);
}

public override void IndexOf(object value, int startIndex, int count) {
lock (__innerObject) {
return (__innerObject.IndexOf(value, startIndex, count);
}
}

is it possible, if i don't lock all the IndexOf methods, that i could get
unexpected results? Or will I be fine since I end up calling a method that
is locked?

Thanks,
Curtis

Nov 15 '05 #2
As Andrew said, this is a possible sync issue. To avoid having to lock the
same lock multiple times, have your public methods lock the sync object and
call a new private method with the (object value, int startIndex, int count)
signature. The private method, will not be "locked" but will need to be
called in a locked context (i.e. from one of your public methods.)

--
William Stacey, MVP

"Curtis Hatter" <cu**********@fuse.net> wrote in message
news:3f***********************@nnrp.fuse.net...
Hi,

Hope this question makes sense. =)

I have an object, and a wrapper for that object that implements a
synchronized view (similar to getting a synchronized ArrayList).

What I'm wondering is, if I need to lock all methods? Here's an example:

I have 3 versions of an IndexOf method:

public override void IndexOf(object value) {
return IndexOf(object value, 0)
}

public override void IndexOf(object value, int startIndex) {
return IndexOf(object value, 0, __innerObject.__count);
}

public override void IndexOf(object value, int startIndex, int count) {
lock (__innerObject) {
return (__innerObject.IndexOf(value, startIndex, count);
}
}

is it possible, if i don't lock all the IndexOf methods, that i could get
unexpected results? Or will I be fine since I end up calling a method that
is locked?

Thanks,
Curtis

Nov 15 '05 #3

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
5
by: Prabu Subroto | last post by:
Dear my friends... I am using SuSE Linux 9.1 and postgres. I am a beginner in postgres, usually I use MySQL. I have 3 tables : appointment, appointment0 and appointment1. the fields of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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...

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.