Question

If I spawn a thread with ThreadPool.QueueUserWorkItem and that thread does not reference the object that is not thread safe, would it compromise that non-thread safe object?

[Edit] By not thread safe object, I mean a third party interface to a programmable logic controller that has no ability to open simultaneous connections or concurrency support.

I suppose I just wanted to be sure that by queuing threads in the same class as my reference to that object, I wouldn't somehow be compromising its thread safeness in a way I didn't realize.

Was it helpful?

Solution

Threads aren't magic. If they don't reference some data, then they can't affect it.


EDIT: If you have code that monitors the creation of new threads, and monitors QueueUserWorkItem, and if the monitor modifies these non thread-safe objects, then yes, there's an impact.

Otherwise, no.

OTHER TIPS

As long as a new thread does not directly or indirectly reference a non-thread safe object, it will not affect it.

Small technical point. Calling ThreadPool.QueueUserWorkItem does not necessarily spawn a new thread. It instead ensures that the provided delegate will be run on a different thread. This may cause a thread to be created, or the more likely case is that it will use an existing thread which is already allocated to the thread pool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top