简体   繁体   中英

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. I am developing in visual Studio 2013, using platform toolset version v120. the reason I need this to be a managed assembly is because it is required to integrate the assembly in LabView.

following the steps in Creating and Using a Managed Assembly in VC++ 2010 gives good results. 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.

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.

error directive: is not supported when compiling with /clr or /clr:pure.

A friend of mine says it is impossible to write multi-threaded code in Visual Studio without using external packages like boost. It kind of seemed unlikely since Multithreading has already been already there for C# and VB for a long time!

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++?

You can use the managed thread library: 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. 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

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. Normally you could resolve this be forward declaring the class in your header and including them only in your cpp files. However you can forward declare your own classes in header files, but you can't for classes in namespace std. According to the C++11 standard, 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.

A workaround for this problem is to create a threading class that inherits from std::thread that you can forward declare. 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:

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

Hope it might help anyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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