This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Read Write locks  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How a read write lock enabled resource (collection) know which are all are read threads and which all are write threads ? How it allows concurrent read?

if I say

read()
{
readlock.lock();
collection.get();
readlock.unlock();
}


write(a){
writeLock.lock();{
collection.put(a);
writelock.unlock();
}



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ganesh Thanga wrote:Hi,
How a read write lock enabled resource (collection) know which are all are read threads and which all are write threads ? How it allows concurrent read?

if I say

read()
{
readlock.lock();
collection.get();
readlock.unlock();
}


write(a){
writeLock.lock();{
collection.put(a);
writelock.unlock();
}



Iin the case you show here, the collection itself knows nothing about its concurrent use, and knows nothing about which threads are reading or writing it, since you're managing that externally.

It's not clear to me what you're having trouble with.
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How a read write lock enabled resource (collection) know which are all are read threads and which all are write threads ?


The collection doesn't know or care if a thread is all read or all write operations. All it knows is which of its own methods are read methods and which of its own methods are write methods. It then protects its own methods with an appropriate lock. So a thread that does all reads will only get read locks, a thread which does all writes gets only write locks, and a thread which reads sometimes and which writes other times will get a read lock when it reads and a write lock when it writes.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!