rubenvb
rubenvb

Reputation: 76721

Exposing a full (C language) pthread interface from a C++ library based on c++0x std::thread

Is it conceptually possible to write a small wrapper library based on std::thread (and perhaps other C++0x parts) that exposes the full pthread interface to a C program?

(If you're wondering how this could be useful: in the hypothetical world where a mainstream OS kernel was written in C++ with a fully C++0x compliant Standard library attached to it, this would be an issue that comes up, because the kernel I'm talking/thinking about does not expose a C interface, only a C Standard library, based on its "native" C++ "backend")

Upvotes: 1

Views: 457

Answers (3)

Anthony Williams
Anthony Williams

Reputation: 68631

Yes, such an implementation should be possible. I even wrote a simple version at one point, just to prove I could.

There are a few things which are tricky, but most stuff (e.g. mutexes, condition variables, threads) would be a simple wrapper.

Asynchronous cancellation is one of the tricky things --- it requires support from the OS to interrupt a thread asynchronously, so true asynchronous cancellation cannot be written on top of "pure" C++0x threads. Of course, you could just delay cancellation to a cancellation point anyway, though as R points out this would be a low quality implementation of the feature.

Upvotes: 1

Howard Hinnant
Howard Hinnant

Reputation: 219185

In libc++ there is:

class thread
{
public:
    // ...
    typedef pthread_t native_handle_type;
    native_handle_type native_handle();
    // ...
};

native_handle_type and native_handle() are conditionally supported. That is, they don't have to be there. However, if they are there, and if it is based on pthreads, then doing what you want to do is why this hook is there.

Upvotes: 1

AProgrammer
AProgrammer

Reputation: 52314

IIRC, there is at least the asynchronous cancellation which has no equivalent in C++0X thread library.

Upvotes: 1

Related Questions