Background
I'm taking an OS class this semester. I negotiated usage of C++ with professor, but I cannot use anything for which there is a linux system call (except memory management).
Problem
To implement interface
template<typename Callable, typename ... ArgTypes>
thread(Callable&& callable, ArgTypes&& ... args)
I need to pass an instantiation of a function template:
template<typename Lambda>
void *call_target(void *target)
{
Lambda *lambda = reinterpret_cast<Lambda *>(target);
(*lambda)();
//don't forget to cleanup
delete lambda;
return nullptr;
}
pthread_create(&thread_handle,
nullptr,
&details::call_target<lambda_type>,
target_function);
but pthread_create
is declared with extern "C"
, so it will have C ABI. I've heard that one cannot pass C++ mangled function through C ABI. Am I correct?
Question
How to pass function template instantiation to extern "C"
function in a portable way? Can I expect that on gcc 5.4 and Ubuntu 16.04 it will work reliably?