In your first code example, if you have multiple threads and they each call worker()
then each thread has a stack on which item
is created and each item
refers to a different element of q_in
.
Also, just to be clear, the function do_work()
cannot modify what item
refers to.
In your second snippet, you have essentially move item
out of the stack and onto the heap where it is associated with an instance of Worker
.
In both cases each thread has its own item
which refers to different instances simultaneously.
Also, for novice programmers, you should consider passing q_in
and q_out
into the function worker()
or each instance of a Worker
rather than having them as global variables.