I was just playing with Intel Parallel inspector on my project, and it displays a warning:
One or more threads in the application accessed the stack of another
thread. This may indicate one or more bugs in your application.
I do indeed have some objects that are allocated on stack shared between threads. I don’t see why this is a problem. Any hints?
Imagine this — a thread is executing and a method is called which has a local (stack) variable (an object). It adds this object to a work queue, a queue which is processed by a separate thread.
That thread gets to the item added by the first thread and accesses the object, on the stack, of the first thread.
What has the first thread done in the meantime? It may have exited the method and freed up that stack space. That freed space may or may not be re-used. The second thread accessing the stack of the first thread may or may not work correctly, depending on timing and the call graph.
If you know the stack variable will exist while the second thread processes it then it can be safe to do; for example, if Thread 1 queues a stack variable and then blocks until Thread 2 notifies it has finished processing, that is a safe operation.
A warning rather than an error is issued because this may or may not be a legitimate operation, and there’s no way for an analyzer to be certain.