Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are some objects not accessible from different threads?

I've come across this problem several times while developing in C#. I'll be a happily coding along, passing objects to and fro between threads and what not, then all of a sudden I get this familiar error:

"The calling thread cannot access this object because a different thread owns it."

Well, ok, I've dealt with it before, especially with objects on the GUI thread. You just have to write some extra code to program around that specific problem. But every once in while I come across an object that is by all means ordinary, yet it doesn't like being accessed by different threads.

EDIT I was mistaken in my original post about the object that was causing the access exception. It was NOT IPAddress, instead its System.Printing.PrintQueue. which I was using to obtain the IP address. This is the object that you can't assess from more than 1 thread.

All my classes I've written never have this problem. I don't even know how I'd implement this myself. Would you have to keep a member variable with the thread ID that created you, and then check the current thread against that on every single property and method access? That seems crazy. Why would Microsoft decide that..... "OK... PrintQueue, definitely not sharable among threads. But these other classes.... their good to go."

Why are some objects blocked from multiple thread access?

like image 239
Ultratrunks Avatar asked Jul 03 '12 19:07

Ultratrunks


People also ask

Can two threads access same object?

Only one thread can execute a method or block of code protected by the same object reference. When you use the synchronized keyword with a method, the object reference is implicit. When you use the synchronized keyword in one or more methods of an object, only one execution thread will have access to all these methods.

Can multiple threads access the same variable?

Only one thread can read and write a shared variable at a time. When one thread is accessing a shared variable, other threads should wait until the first thread is done. This guarantees that the access to a shared variable is Atomic, and multiple threads do not interfere.

How do you share an object between threads?

There are many ways to share same Object between multiple threads. You can use a BlockingQueue to pass an Object from one thread to another thread. You can also use Exchanger class for this purpose. An Exchanger is a bidirectional form of a SynchronousQueue in Java.

What do you mean by thread-safe?

Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without unintended interaction.


1 Answers

I think this may explain things fairly well, I think this specifically has to do with COM.

http://msdn.microsoft.com/en-us/library/ms693344%28v=vs.85%29

specifically.

In general, the simplest way to view the COM threading architecture is to think of all the COM objects in the process as divided into groups called apartments. A COM object lives in exactly one apartment, in the sense that its methods can legally be directly called only by a thread that belongs to that apartment. Any other thread that wants to call the object must go through a proxy.

There are two types of apartments: single-threaded apartments, and multithreaded apartments.

Single-threaded apartments consist of exactly one thread, so all COM objects that live in a single-threaded apartment can receive method calls only from the one thread that belongs to that apartment. All method calls to a COM object in a single-threaded apartment are synchronized with the windows message queue for the single-threaded apartment's thread. A process with a single thread of execution is simply a special case of this model.

Multithreaded apartments consist of one or more threads, so all COM objects that live in an multithreaded apartment can receive method calls directly from any of the threads that belong to the multithreaded apartment. Threads in a multithreaded apartment use a model called free-threading. Calls to COM objects in a multithreaded apartment are synchronized by the objects themselves.

like image 129
Matthew Vines Avatar answered Sep 19 '22 16:09

Matthew Vines