Windows上的C11线程
C11 threads on Windows
我正在Windows上创建Visual Studio 2012 express的跨平台软件。由于显而易见的原因,我不能使用。net的System::Threading::Thread
。我希望我能使用C11 (threads.h
,而不是pthread.h
)的新线程功能,而使用VS2012,因为我创建了一个基于。net表单的抽象框架。我开始相信这对Windows来说是不可能的。有人有什么主意吗?我只会使用c++库(boost和std),如果它们是我唯一的选择。
有人知道该怎么做吗?
Visual Studio 2012不支持C11的线程(微软已经反复声明,它没有兴趣与C保持同步,更愿意专注于c++),但它确实支持c++ 11的std::线程和相关设施。如果你正在编写c++,你应该使用它们而不是C的线程库。
Visual Studio 2017包含一个标题xthreads.h
,它与threads.h
非常相似,但略有不同。例如:
#include <threads.h>
#include <time.h>
#include <stdio.h>
int main(void)
{
printf("Time: %s", ctime(&(time_t){time(NULL)}));
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
printf("Time: %s", ctime(&(time_t){time(NULL)}));
}
将#include <thr/xthreads.h>
#include <time.h>
#include <stdio.h>
int main(void)
{
struct xtime stoptime;
xtime_get( &stoptime, 1);
stoptime.sec += 1;
printf("Time: %s", ctime(&(time_t){time(NULL)}));
_Thrd_sleep( &stoptime );
printf("Time: %s", ctime(&(time_t){time(NULL)}));
}
*注:xthreads.h
不是标准,因此可能会发生变化。*
也有一个仿真库在https://gist.github.com/yohhoy/2223710 .
C11线程接口主要是从Dikumware专有线程库中的线程接口复制过来的。他们的东西在不同的平台上运行,他们创建了一个接口,作为Windows线程和POSIX线程功能的交集。
他们现在是否把它作为"官方的"C11线程库,我不知道,但应该离它不远了。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- Windows上的C11线程