从基于c ++ 0x std :: thread的C ++库中暴露一个完整的(C语言)pthread接口 [英] Exposing a full (C language) pthread interface from a C++ library based on c++0x std::thread
问题描述
从概念上讲,可以基于 std :: thread
(或许还有其他C ++ 0x部分)编写一个小的封装库,它将完整的pthread接口暴露给C程序?
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?
(如果你想知道这是如何有用的:在假设的世界里,一个主流的操作系统内核是用C ++编写的,这将是一个问题,因为我正在说话/思考的内核不暴露一个C接口,只有一个C标准库,基于其本地C ++后端)
(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")
推荐答案
是的,这样的实现应该是可能的。我甚至在一个点上写了一个简单的版本,只是为了证明我可以。
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.
异步取消是一个棘手的事情---它需要操作系统支持异步中断一个线程,所以真正的异步取消不能写在纯C ++ 0x线程的顶部。当然,你可以只是延迟取消取消点,无论如何,虽然R指出这将是一个低质量的功能的实施。
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.
这篇关于从基于c ++ 0x std :: thread的C ++库中暴露一个完整的(C语言)pthread接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!