• Post Reply Bookmark Topic Watch Topic
  • New Topic

threads, iteration and removal

 
Lee Barney
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to be able to allow iteration over a collection by multiple concurrent threads, but if a removal method is called then all new iterations must wait until the removal happens. Also, the removal must wait until all of the threads iterating over the collection have completed.

Ideas?
 
Lee Barney
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this. Something I just thought of.

Create a fair semaphore that has Integer.MAX_VALUE permits.

When each iteration begins, aquire a single permit and release it when iteration is complete.

When each removal begins, aquire Integer.MAX_VALUE permits and release them after the removal is complete.

The removal will happen in fifo order since the semaphore is initialized as fair and will not begin until all iteration requests prior to the request for the removal are complete. In addition to this, all new iteration or removal requests will wait until the removal is complete.

I know that a fair semaphore is not as fast as a non-fair one.

Any other ideas?
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lee Barney:
How about this. Something I just thought of.

Create a fair semaphore that has Integer.MAX_VALUE permits.

When each iteration begins, aquire a single permit and release it when iteration is complete.

When each removal begins, aquire Integer.MAX_VALUE permits and release them after the removal is complete.

The removal will happen in fifo order since the semaphore is initialized as fair and will not begin until all iteration requests prior to the request for the removal are complete. In addition to this, all new iteration or removal requests will wait until the removal is complete.

I know that a fair semaphore is not as fast as a non-fair one.

Any other ideas?


What you are basically describing is a reader-writer lock. You are just simulating the functionality with a semaphore. A couple of notes:

1. Grabbing MAX_VALUE permits is fine provided that you release the original one. Or you will get a case, where 2 threads each hold one permit, but is waiting for MAX_VALUE permits.

2. And I not sure, but I don't think the iterator will work in parallel. It may still throw a Concurrent Exception, if it encounters something that it did not directly change.

Henry
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest solution that I can think of is to delegate all operations on your collection to a class. Now, instead of sharing the collection itself among all the threads, you would share an instance of that class:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!