简体   繁体   English

如何制作托管(clr)多线程c ++ .dll?

[英]How to make a managed (clr) multithreaded c++ .dll?

I am trying to make a managed .dll in c++ that requires the support for multithreading. 我试图在c ++中创建一个需要支持多线程的托管.dll。 I am developing in visual Studio 2013, using platform toolset version v120. 我正在使用平台工具集版本v120在visual Studio 2013中进行开发。 the reason I need this to be a managed assembly is because it is required to integrate the assembly in LabView. 我需要将其作为托管程序集的原因是因为需要在LabView中集成程序集。

following the steps in Creating and Using a Managed Assembly in VC++ 2010 gives good results. 按照在VC ++ 2010创建和使用托管程序集中的步骤, 可以获得良好的结果。 but I obviously need to implement something more complicated and when I include threading and write the following code: 但我显然需要实现更复杂的东西,当我包含线程并编写以下代码时:

#pragma once
#include <thread>

using namespace System;
using namespace std;


namespace MultiThread_module {

    public ref class multiThreadingTest
    {
    public:
        String^ GetVersion();
        int someNumber;

    private:

        thread testThread;
    };
}

I get following errors: 我收到以下错误:

"thread" is not supported when compiling with /clr or /clr:pure. 使用/ clr或/ clr:pure进行编译时,不支持“thread”。

a member of a managed class cannot be of a non-managed class type 托管类的成员不能是非托管类类型

error directive: ERROR: Concurrency Runtime is not supported when compiling /clr. 错误指令:错误:编译/ clr时不支持并发运行时。

error directive: is not supported when compiling with /clr or /clr:pure. 错误指令:使用/ clr或/ clr:pure进行编译时不支持。

A friend of mine says it is impossible to write multi-threaded code in Visual Studio without using external packages like boost. 我的一位朋友说,如果不使用像boost这样的外部软件包,就不可能在Visual Studio中编写多线程代码。 It kind of seemed unlikely since Multithreading has already been already there for C# and VB for a long time! 这似乎不太可能,因为多线程已经在C#和VB已经存在很长时间了!

So, I would be happy if you could let me know what I am doing wrong OR if it is really hard to have a managed multithreaded .dll developed in c++? 所以,如果你能让我知道我做错了什么, 或者如果用c ++开发的托管多线程.dll真的很难,我会​​很高兴的吗?

You can use the managed thread library: System.Threading.Thread . 您可以使用托管线程库: System.Threading.Thread

#pragma once

using namespace System;
using namespace std;
using namespace System::Threading;


namespace MultiThread_module {

    public ref class multiThreadingTest
    {
    public:
        String^ GetVersion();
        int someNumber;

    private:

        Thread^ testThread;
    };
}

If it's purely CLR then I suggest you use the example provided before. 如果它纯粹是CLR,那么我建议您使用之前提供的示例。 If you want to have the threading completely native and just use CLR to wrap it, I'd like to refer you to my answer at : using clr and std::thread 如果你想让线程完全是原生的并且只是使用CLR来包装它,我想引用你的回答: 使用clr和std :: thread

Might be an old question, but I looked into this same problem before. 可能是一个老问题,但我之前研究过同样的问题。 Since CLR does not allow you to include std::thead at compile time, you could try to use it only at linking time. 由于CLR不允许您在编译时包含std :: thead ,因此您可以尝试仅在链接时使用它。 Normally you could resolve this be forward declaring the class in your header and including them only in your cpp files. 通常你可以解决这个问题,在你的标题中声明类,并将它们仅包含在你的cpp文件中。 However you can forward declare your own classes in header files, but you can't for classes in namespace std. 但是,您可以在头文件中转发声明自己的类,但不能用于名称空间std中的类。 According to the C++11 standard, 17.6.4.2.1: 根据C ++ 11标准,17.6.4.2.1:

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. 如果C ++程序向命名空间std或命名空间std中的命名空间添加声明或定义,则它是未定义的,除非另有说明。

A workaround for this problem is to create a threading class that inherits from std::thread that you can forward declare. 此问题的解决方法是创建一个继承自std :: thread的线程类,您可以转发声明。 The header file for this class would look like: 此类的头文件如下所示:

 #pragma once #include <thread> #include <utility> 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: }; } 

In the header file that you would like to use the thread you can do forward declare it like: 在您想要使用线程的头文件中,您可以向前声明它,如:

 #pragma once // Forward declare the thread class namespace Threading { class Thread; } class ExampleClass { public: ExampleClass(); void ThreadMethod(); private: Threading::Thread * _thread; }; 

In your source file you can then use the theading class like: 在源文件中,您可以使用theading类,如:

 #include "ExampleClass.h" #include "Thread.h" ExampleClass::ExampleClass() : { _thread = new Threading::Thread(&ExampleClass::ThreadMethod, this); } void ExampleClass::ThreadMethod() { } 

Hope it might help anyone. 希望它可以帮助任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM