Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Question about worker_routine

christiecohen
Beginner
259 Views
I read into the source codes and find that the worker thread is created like this:

int status = pthread_create( &thread_handle, NULL, GenericScheduler::worker_routine, this );


But the worker_routine is defined like this:

void* GenericScheduler::worker_routine( void* arg )
{
GenericScheduler& scheduler = *create_worker(*(WorkerDescriptor*)arg);
ITT_NAME_SET(thr_name_set, "TBB Worker Thread", 17);
pthread_cleanup_push( cleanup_worker_thread, &scheduler );
scheduler.wait_for_all(*scheduler.dummy_task,NULL);
pthread_cleanup_pop( true );
return NULL;
}

So I'm confused how the executing codes mapped to the worker_routine.

I know the Task's execute() is called in wait_for_all():

task* t_next = t->execute();


But how the execute() wasrelated toone worker thread to run on it.


Thanks
0 Kudos
3 Replies
Alexey-Kukanov
Employee
259 Views
A worker thread runs task dispatch loop in wait_for_all. The loop first takes and executes (i.e. calls execute()) tasks in the local pool of the thread, and if no tasks there (which is initial state for worker threads) it tries to steal a task from the pool of another thread.
Tasks are placed into task pools by spawn_* methods.

Does this information answer your question on relation between tasks and worker threads?
0 Kudos
christiecohen
Beginner
259 Views
A worker thread runs task dispatch loop in wait_for_all. The loop first takes and executes (i.e. calls execute()) tasks in the local pool of the thread, and if no tasks there (which is initial state for worker threads) it tries to steal a task from the pool of another thread.
Tasks are placed into task pools by spawn_* methods.

Does this information answer your question on relation between tasks and worker threads?

Thanks, Alexey.

But actually not.
I read the code there are 3 loops.....and the t-execute() is called.

I mean, for linux platform,usually when we create a thread to do something we first define a function,for example:funcA(). Then we use: "pthread_create( &thread_handle, NULL, funcA, this )" to create a threadto run it.
Right?

To your implementation, I thinkit needs to dynamically pass the execute() function to the worker thread.
You use:
pthread_create( &thread_handle, NULL, GenericScheduler::worker_routine, this );
So the worker_routine should be the execute() function.
Am I right?

Sohow worker_routine related to execute() function?
I don't find any code shows their connection.

If I'm wrong, please tell me how you do that with pthread.
0 Kudos
Alexey-Kukanov
Employee
259 Views
You are right there should be a thread function, but this function should not be task::execute().
Tasks are not threads; there is no one-to-one mapping between a task and a thread. Actually, the mapping is one (thread) to many (tasks).
The function being executed by a thread is worker_routine; it calls the task dispatch loop routine (wait_for_all) which works as I described above; this is where task::execute() is called.
0 Kudos
Reply