Windows上的C11线程

问题描述 投票:3回答:3

我正在Windows上的Visual Studio 2012 Express中创建跨平台软件。出于显而易见的原因,我无法使用.NET的System::Threading::Thread。我希望我可以使用C11的新线程功能(threads.h,而不是pthread.h),同时使用VS2012,因为我创建了一个基于.NET表单的抽象框架。我开始相信Windows是不可能的。有人有想法吗?如果这些是我唯一的选择,我将只使用C ++库(boost和std)。

有谁知道该怎么办?

c++ windows multithreading c11
3个回答
9
投票

Visual Studio 2012不支持C11的线程化(微软已多次声明它对保持与C的关联没什么兴趣,更喜欢专注于C ++),但它确实支持C ++ 11的std::thread and related facilities。如果你正在编写C ++,你应该可以使用它们而不是C的线程库。


1
投票

Visual Studio 2017包含一个标题xthreads.h,它与threads.h非常相似但略有不同。例如:

来自https://en.cppreference.com/w/c/thread/thrd_sleep

#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还有一个仿真库。


0
投票

C11线程接口主要是从Dikumware的线程接口复制到它们的propretary线程库中。 AFAIR他们的东西在不同的平台上运行,他们创建了该接口作为Windows线程和POSIX线程的功能的交集。

他们是否已经将其作为“官方”C11线程库,我不知道,但它应该离它不远。

© www.soinside.com 2019 - 2024. All rights reserved.