Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用clr和std::thread_C++_.net_Stdthread - Fatal编程技术网

C++ 使用clr和std::thread

C++ 使用clr和std::thread,c++,.net,stdthread,C++,.net,Stdthread,我正在为桌面创建一个UI抽象层。现在我正在实现.NET framework的功能。恼人的是,如果我让用户在Visual studio中创建CLR Windows窗体应用程序,他们就不能使用所有标准库,如std::thread,如果我让他们创建另一种类型的应用程序,控制台就会出现 有没有办法将clr与std::thread一起使用,或者更好的办法是,有没有办法防止控制台启动(或者在屏幕和任务栏上隐藏)clr控制台或clr空项目 谢谢这是一个老问题,但如果有人遇到同样的问题:boost::threa

我正在为桌面创建一个UI抽象层。现在我正在实现.NET framework的功能。恼人的是,如果我让用户在Visual studio中创建CLR Windows窗体应用程序,他们就不能使用所有标准库,如
std::thread
,如果我让他们创建另一种类型的应用程序,控制台就会出现

有没有办法将clr
std::thread
一起使用,或者更好的办法是,有没有办法防止控制台启动(或者在屏幕和任务栏上隐藏)clr控制台或clr空项目


谢谢

这是一个老问题,但如果有人遇到同样的问题:
boost::thread
是一个“价格合理”且实用的替代品(前提是您可以在项目中使用boost)。奇怪的是,它绕过了不兼容性。

可能是个老问题,但我以前也研究过这个问题。由于CLR不允许您在编译时包含
std::thead
,所以您可以尝试仅在链接时使用它。通常,您可以通过在头文件中声明类并仅在cpp文件中包含它们来解决此问题。但是,可以在头文件中向前声明自己的类,但是不能在命名空间标准中声明类。根据C++11标准,17.6.4.2.1:

< > C++程序的行为如果添加声明或是声明,则未定义。 命名空间std或命名空间std中的命名空间的定义 除非另有规定

解决此问题的一个方法是创建一个线程类,该类继承自
std::thread
,您可以向前声明该类。此类的头文件如下所示:

#pragma once
#include <thread>
namespace Threading
{
    class Thread : std::thread
    {
    public:
        template<class _Fn, class... _Args> Thread(_Fn fn, _Args... args) : std::thread(fn, std::forward<_Args>(args)...)
        {

        }
    private:

    };
}
然后,您可以在源文件中使用如下所示的编码类:

#include "ExampleClass.h"
#include "Thread.h"

ExampleClass::ExampleClass() :
{
    _thread = new Threading::Thread(&ExampleClass::ThreadMethod, this);
}

void ExampleClass::ThreadMethod()
{
}

希望它能对任何人有所帮助。错误似乎强烈地暗示了<代码> >代码>不支持CLR:< C++ >代码错误:错误:编译/CLR时不支持并发运行时。.NET也有线程。是的,这就是为什么我在寻找替代方案。只是不要用CL/CLR编译本机C++代码。使用C++/CLI的意义在于它可以同时做到这两个方面。这不是一个选项,因为我希望用户能够毫无问题地将此代码集成到自己的代码中。像std和boost这样的常用和通用库应该兼容。

#include "ExampleClass.h"
#include "Thread.h"

ExampleClass::ExampleClass() :
{
    _thread = new Threading::Thread(&ExampleClass::ThreadMethod, this);
}

void ExampleClass::ThreadMethod()
{
}