Skip to content

How to deal with fftw thread callback called recursively #388

@milasudril

Description

@milasudril

I noticed that the thread callback routine may be called recursively. This is problematic when using a simple thread_pool that accepts jobs in a FIFO order. It deadlocks because it is waiting on a task that cannot be executed yet.

My code is currently

fftwf_init_threads();
fftwf_plan_with_nthreads(static_cast<int>(workers.max_concurrency()));
fftwf_threads_set_callback(
	[](void *(*work)(char*), char* jobdata, size_t elsize, int njobs, void* workers)
 	{
		signaling_counter counter{static_cast<size_t>(njobs)};
		auto& obj = *static_cast<thread_pool_type*>(workers);
		for (int i = 0; i < njobs; ++i)
		{
			obj.submit(
				[work, jobdata, elsize, i, &counter = counter.get_state()]{
					work(jobdata + elsize * i);
					counter.decrement();
				}
			);
		}
		counter.wait();
	},
	&workers
);

What is the approach I should take to avoid deadlock?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions