Giving two threads access to a common datastructure

  • Thread starter Thread starter David Troy
  • Start date Start date
D

David Troy

I have two threads:

- thread A will perform a long running I/O operation and update some
variables in memory based on reading from the I/O

- thread B should be able to get read-only access to thread A's
variable's at any given time

What is the best way to do this? I thought I could define a
datastructure in Thread A and pass it to thread B, but I'm not sure
how to get that through the ThreadStart method and have it be defined.

Any help appreciated!

Thanks,
Dave
 
I believe you do not have to do anything special for simple types (like int,
etc.).
As far as I know there are no restrictions for memory access within the
process.
Just declare some global variable and use it.
It may not be safe when you are using properties of some complex objects,
because using a property is about calling it's get function and thus it
depends on the internal logic of the function.
 
Hi Dave,

I agree with Max that you can declare them as global variables and write to
them from one thread and read from other threads. In addition, please make
sure that other threads will not write the data. Otherwise, you will need
to use a synchronization classe (say ReaderWriterLock, Mutext, etc) to
protect the shared resource.

Threading Objects and Features
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconthreadingobjectsfeatures.asp

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top