Is it possible to get the ID of the worker thread that runs my spawned function?
You can call any OS function that will return the current thead ID. For example, GetCurrentThreadId() on Windows. But you shouldn't depend on it.
You can also get the worker ID. But again, you shouldn't depend on it.
Why do you want it?
I have an array of data structures that I want to reuse for all the functions of the same worker thread.Just as an example, if I want as a user to maintain an array where in index k I count the number of functions that ran on worker thread k, how can I do it?
> I have an array of data structures that I want to reuse for all the functions of the same worker thread.
You should use a holder reducer. Creating a holder is described in the documentation.
> Just as an example, if I want as a user to maintain an array where in index k I count the number of>functions that ran on worker thread k, how can I do it?
You can get the worker ID by calling cilk::current_worker_id(), but it's a bad habit to get into. You should not be depending on which worker a function is run on. Using the worker ID for anything more than debugging or cosmetic stuff is frequently a sign that you'rethinking about the problem as if you were using OpenMP.
In general, reducers provide a much better way of doing things that need to be kept separate on each worker and then combined for the final result. Properly written reducers are race free, lock free, and give you results that are the same for serial and parallel runs.
Well, I want each spawned function to have its own (large) scratch array, which it only uses during its operation. When the spawned function returns, this scratch can be reused.How do you suggest to do this?
That's exactly what a holder is for. A holder is a reducer where the reduce operation is a noop.
See the chapter on "Writing Reducers - A 'Holder' Example" for all thegory details. It's under "How to Develop a New Reducer", which is under "Reducers"