How to create managed (clr) multithreaded C ++. Dll?

I am trying to create a managed .dll in C ++ that requires multithreading support. I am developing in Visual Studio 2013 using the v120 platform toolbox. the reason I need it to be a managed assembly is because it requires an assembly to integrate into 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 turn on 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 am getting the following errors:

"thread" is not supported when compiled with / clr or / clr: pure.

member of a managed class cannot be of an unmanaged type

error directive: ERROR: Concurrency Runtime not supported if compiling / clr.

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

A friend of mine says that it is not possible to write multi-threaded code in Visual Studio without using external packages like boost. This seemed unlikely since Multithreading has been around for C # and VB for a long time!

So, I would be glad if you could let me know what I am doing wrong OR if it is really difficult to create a managed multithreaded .dll developed in C ++?

+3


source to share


2 answers


You can use the managed threads 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;
    };
}

      

+1


source


If it's purely CLR, I suggest you use the above example. If you want threading to be completely native and just use the CLR to wrap it, I would like to direct you to my answer at using clr and std :: thread



Maybe an old question, but I've looked into this issue before. Since the CLR does not allow std :: thead to be included at compile time, you can try to use it only when binding in time. As usual you can solve this problem to declare the class in the header and only include them in your cpp files. However, you can forward declare your own classes in header files, but you cannot for classes in the std namespace. According to C ++ 11 standard, 17.6.4.2.1:

     

The behavior of a C ++ program is undefined if it adds declarations or definitions to the std namespace or to the namespace in the std namespace unless otherwise noted.

A workaround for this problem is to create a thread class that inherits from std :: thread, which you can forward. The file header for this class will look like this:

#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 you want to use, you can do ahead 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 use the adading class, for example:

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

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

void ExampleClass::ThreadMethod()
{
}

      

Hope this can help anyone.

0


source







All Articles