Thread safe

  • Thread starter Thread starter Newbee
  • Start date Start date
N

Newbee

Looking for some suggetions on the following..

I have an array that gets filled by multiple application. Same instance
of this array(static array) will be accessed by multiple applications.

ie there will be applications that fill this array and som application
applications that retrieves elements one by one and process and delete
entries from the array. How do I ensure thread safety for this array?

Thanks in advance
 
IMO WebService will be inefficient in this scenario.

you can use simple lock statement in the server, and wrap the access to
array.

e.g.

public int Count
{
get { lock(this) { return ( _array.Length ); } }
}
 
Vadym said:
IMO WebService will be inefficient in this scenario.

you can use simple lock statement in the server, and wrap the access to
array.

e.g.

public int Count
{
get { lock(this) { return ( _array.Length ); } }
}
This really would not be a lot of help. You lock the object then get the
length and then return. Now he has a length that might change in the
next fraction of a second by another thread so any logic he does on it
would be in error.

You should design an object to manage your data. The applications should
never get direct access to the array. This smart object should then have
methods to support the business logic that the applications need. The
methods would do their work within a lock block to ensure thread safety.
The applications should know nothing about locking the data structures.
Just the new manager object should. Another good practice is to use
queues for requesting work of the manager object in case it needs to do
some background calculations. This is a good way to avoid deadlock
situations.

Hope this help.
Leon Lambert
 
Back
Top