Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(B&S) Is my lock() thread safe?

 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Here is a pseudocode of my lock()

Assume that validateRecord() checks if a record is valid(I use a cache:So it checks a cache). I dont know if it is ok?Thanks!
 
Jeff Powell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may have a problem with the lock, I don't see where you have clients wait if the lock is already in use.


public long lock(int recNo) throws RecordNotFoundException {
long cookie = 0;
//validate record

synchronized(reservedContractors) // I assume this is where you reserve the list of locked records.
{
}
where do you wait if the record is already locked?
The specification should say if a record is locked, the thread must give up control without using any cycles.
//validate recNo still exists
//generate a valid lockcookie
//place in HashMap
}
return cookie;
}

Also I don't understand why you validate the record twice. If it can be put on the list called reservedContractors, why not wait till after you have exclusive access to the record and then check to see if it exists. If it does not exist, unlock it and throw an exception. If it does go on, but don't validate it before you have exclusive access. I also did not use a cache system so you may have to validate twice and I not getting it in the code.

Hope this helps
 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Powell:
I think you may have a problem with the lock, I don't see where you have clients wait if the lock is already in use.


public long lock(int recNo) throws RecordNotFoundException {
long cookie = 0;
//validate record

synchronized(reservedContractors) // I assume this is where you reserve the list of locked records.
{
}
where do you wait if the record is already locked?
The specification should say if a record is locked, the thread must give up control without using any cycles.
//validate recNo still exists
//generate a valid lockcookie
//place in HashMap
}
return cookie;
}

Also I don't understand why you validate the record twice. If it can be put on the list called reservedContractors, why not wait till after you have exclusive access to the record and then check to see if it exists. If it does not exist, unlock it and throw an exception. If it does go on, but don't validate it before you have exclusive access. I also did not use a cache system so you may have to validate twice and I not getting it in the code.

Hope this helps


Thanks for the reply
I agree with u, i should have exclusive right to the cache before checking if the record exists! Within the synchronised blocks the client checks while the RecNo is within the collection(HashMap), then "waits" if not, it places with within the collection: So now my pseudocode looks like this:


I dont know if i can post this much! But please delete if necessary Mr Monkhouse! And i would like to know if this is thread Safe?Thanks
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saheed,

Are you aware that you could end up locking a deleted record?

Regards, Andrew
 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for the insight! My validateRecord() method is synchronized! In the method the client syncs on the cache before it checks if it is valid.I dont know if this is enough to ensure that it is thread safe!Or isnt it?
Thanks for push Andrew!
 
Alan Morgan
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saheed,

Think more in terms of a sequence.
I validate record and it is ok, but then I have to wait.
When I wake up I must check the condition again as it is in a while loop.
So this time I gain access and continue.
Could anything have happened while the thread was asleep to make this sequence potentially dangerous ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic