Do thread queues affect non-thread objects in the same class?

If I create a thread with ThreadPool.QueueUserWorkItem and that thread is not referencing an object that is not thread safe, could it compromise that non thread safe object?

[Edit] Unsafe object with thread, I mean a third party interface to a programmable logic controller that has no ability to open concurrent connections or maintain concurrency.

I suppose I just wanted to make sure that by queuing threads in the same class as my reference to that object, I would not in any way compromise thread safety in a way that I did not understand.

0


a source to share


2 answers


Threads are not magic. If they do not link to some data, they cannot influence it.




EDIT: If you have code that monitors the creation of new threads and monitors the QueueUserWorkItem, and if the monitor modifies these unsafe objects, then yes, there is an impact.

Otherwise, no.

+2


a source


As long as the new thread does not directly or indirectly refer to the non-thread safe object, it will not affect it.



A small technical point. Calling ThreadPool.QueueUserWorkItem does not necessarily spawn a new thread. Instead, it ensures that the provided delegate will run on a different thread. This could result in a thread being created, or most likely it will use an existing thread that is already allocated to the thread pool.

0


a source







All Articles