about multithreading locking a resource for access

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

Here is some text from e-learning that I find strange. It says:

"You can configure locks so that only a single thread may access the
resource at a given time or so that a specified number of threads may access
the resource simultaneously."

Now why on earth would anyone want to lock a resource so that a specified
number of threads may access the resource simultaneously.

This sound strange becuse that would cause a lot of error whan several
threads interfere with each other operation.
For example if several threads update an integer in a class you will get a
result that probably is wrong.

So can somebody explain why the text write so that a specified number of
threads may access the resource simultaneously.

//Tony
 
Hi,

Access doesn't mean update. Some threads could only read values written by
other threads in which case mutliple threads can read but only a single one
can write.

Try the doc I mentioned earlier :
http://msdn.microsoft.com/en-us/library/ms228964(v=VS.90).aspx

It summarize available synchronization options and mention those allowing
this such as ReaderWriterLockSlim or Semaphore classes. Checking the
documentation in addition to your book would be IMO really helpfull...
 
Here is some text from e-learning that I find strange. It says:

"You can configure locks so that only a single thread may access the
resource at a given time or so that a specified number of threads may access
the resource simultaneously."

Now why on earth would anyone want to lock a resource so that a specified
number of threads may access the resource simultaneously.

This sound strange becuse that would cause a lot of error whan several
threads interfere with each other operation.
For example if several threads update an integer in a class you will get a
result that probably is wrong.

So can somebody explain why the text write so that a specified number of
threads may access the resource simultaneously.

If I were to guess, then they are thinking about the case of
multiple readers.

Arne
 
Tony said:
Hi!

Here is some text from e-learning that I find strange. It says:

"You can configure locks so that only a single thread may access the
resource at a given time or so that a specified number of threads may access
the resource simultaneously."

Now why on earth would anyone want to lock a resource so that a specified
number of threads may access the resource simultaneously.

This sound strange becuse that would cause a lot of error whan several
threads interfere with each other operation.
For example if several threads update an integer in a class you will get a
result that probably is wrong.

So can somebody explain why the text write so that a specified number of
threads may access the resource simultaneously.

One case is when you have more than one unit of resource, i.e., a pool of
resources. Consider the thread pool itself, which at any given moment has a
fixed size. If the current size is, e.g., 25 threads then the manager of that
thread pool resource can service up to 25 concurrent requests before someone has
to wait for a unit of the resource to become available.

-rick-
 
Back
Top