Visual Studio 2017 几乎没有 C99 和 C11 语言支持,但在某些领域,它似乎具有相似的底层原语,但具有原语名称的修改版本。
具体来说,我正在研究 C11 的多线程库的支持。
在查看包含文件 thr/xthreads.h 时,大多数 C11 多线程库原语都可用,但函数名称以下划线作为前缀。换句话说,thrd_create()
是_Thrd_create()
,并且包含文件threads.h
不存在,但thr/xthreads.h
确实如此。
阅读 Herb Sutter 发表于 2012 年 5 月 3 日的博文,Reader Q&A: What about VC++ and C99? ,看来微软几乎没有计划在 Visual Studio 中实现 C99 或 C11,尽管互联网上的其他评论和问题表明这一立场自 2012 年以来可能已经发生了变化。
持续支持包含文件 thr/xthreads.h
中声明的多线程库的可能性有多大?
我有什么保证这些原语将在 Visual Studio 2019 及更高版本中得到维护?是否有关于 Microsoft Visual Studio 计划的任何其他信息?
在 Visual Studio 2017 中创建 Windows 控制台应用程序后,我将以下简单示例放在 .c 扩展源文件中。 main()
位于 .cpp 源文件中,由以下部分组成对下面的 func()
函数的调用:
#include <stdio.h>
#include <thr/xthreads.h> // access the underlying C11 threads library.
typedef struct {
int ik;
int ij;
_Thrd_t *p;
} Thing;
int tx1 ( void * y)
{
Thing * x = (Thing *)y;
xtime xTimeAmt = {0};
printf("One this is thread %d %d\n", x->ik, x->ij);
_Thrd_yield();
xtime_get(&xTimeAmt, TIME_UTC);
xTimeAmt.sec += 5; // wait 5 seconds
_Thrd_sleep(&xTimeAmt);
printf("Two this is thread %d %d\n", x->ik, x->ij);
return 0;
}
_Thrd_t tt1;
_Thrd_t tt2;
_Thrd_t tt3;
int func(void)
{
Thing t1 = { 1, 10 };
Thing t2 = { 2, 20 };
Thing t3 = { 3, 30 };
int rt1, rt2, rt3;
for (int i = 3; i < 10; i++) {
printf("i = %d\n", i);
}
_Thrd_create((t1.p = &tt1), tx1, &t1);
_Thrd_create((t2.p = &tt2), tx1, &t2);
_Thrd_create((t3.p = &tt3), tx1, &t3);
_Thrd_join(tt1, &rt1);
_Thrd_join(tt2, &rt2);
_Thrd_join(tt3, &rt3);
for (int i = 23; i < 30; i++) {
printf("i = %d\n", i);
}
return 0;
}
该程序的输出是:
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
One this is thread 1 10
One this is thread 2 20
One this is thread 3 30
Two this is thread 1 10
Two this is thread 3 30
Two this is thread 2 20
i = 23
i = 24
i = 25
i = 26
i = 27
i = 28
i = 29
最佳答案
How likely is there to be continued support for the multi-threading library declared in the include file thr/xthreads.h?
不太可能。
What assurances do I have that these primitives will be maintained in Visual Studio 2019 and later?
没有。
Is there any other information available as to Microsoft plans for Visual Studio?
嗯,如the standard states :
Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.)
您永远不应该依赖他们的任何行为。引用a relevant MS bug report :
Like all underscore-capital _Ugly machinery, such bits may be added, changed, and removed in any update (unless the machinery is a specifically documented exception, like _ITERATOR_DEBUG_LEVEL, or basic_string::_Copy_s).
他们还说明了您应该做什么:
You should call documented APIs, like
std::thread
or_beginthreadex
, instead.
关于c - Visual Studio C 的 _Thrd_create() 和其他线程函数是 Thrd_ C11 函数的实现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60172981/