How can I mix the Concurrency Runtime with .NET code?

前端 未结 5 1312
旧时难觅i
旧时难觅i 2021-01-06 04:14

I\'ve been using the Concurrency Runtime in a C++ static library, and recently wanted to use this library in a C++/CLI project, to take advantage of the Windows Form designe

相关标签:
5条回答
  • 2021-01-06 04:24

    You could consider writing a managed GUI, and have it invoke (using PInvoke) an unmanaged DLL: if you can package the Concurrency Runtime, and the code which uses it, as a DLL.

    0 讨论(0)
  • 2021-01-06 04:29

    Using ConcRT in C++/CLI is explicitly disabled in concrt.h via the statement below because it is not officially supported...

    #if defined(_M_CEE)
       #error ERROR: Concurrency Runtime is not supported when compiling /clr.
    #endif
    

    You can use PInvoke to work around this as suggested above, or you can also use the pointer to implementation idiom to address this by forward declaring a 'pimpl' class and hide the dependency on concrt.h to a native .cpp file which you can then compile into a lib and link against with the header file.

    e.g. in the .h file:

    //forward declaration
    class PImpl;
    
    class MyClass
    {
      ....
      //forward declaration is sufficient because this is a pointer
      PImpl* m_pImpl;
    }
    

    e.g. in your .cpp file which compiles into a native lib:

      #include <ppl.h>
      class PImpl
      {
       //some concrt class
       Concurrency::task_group m_tasks;
      }
    
    0 讨论(0)
  • 2021-01-06 04:40

    I'm not sure how detailed your concurrent needs are, but OpenMP works fine (i.e., you can combine options /clr and /openmp)

    array<MyModelResult^>^ model ....;
    #pragma omp parallel for
    for(int i=0;i<model->Length;i++) {
        model[i] = ComputeModelFor(i);
    }
    
    0 讨论(0)
  • 2021-01-06 04:44

    I faced the same issue while while linking C++ to C# using CLR. This issue was caused while directly referencing the below items in the header file(*.h) which was included in CLR project.

    #include <ppl.h>
    using namespace concurrency;
    

    Since CLR does not support concurrency this gives error in the CLR project build. Moving this two lines to the corresponding *.cpp file solved the issue.

    0 讨论(0)
  • 2021-01-06 04:46

    Even if ConcRT in C++/CLI is explicitly disabled, you can have your project compiled with clr support and have some native classes in the same project, by setting CompileAsManaged property to false and PrecompildHeader to NotUsing in your vcxproj file (I've tested this with VS2013):

    <ClCompile Include="NativeProcessWithThread.cpp">
      <CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsManaged>
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
      <PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
      </PrecompiledHeaderFile>
      <PrecompiledHeaderOutputFile Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
      </PrecompiledHeaderOutputFile>
      <CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsManaged>
      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
      <PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
      </PrecompiledHeaderFile>
      <PrecompiledHeaderOutputFile Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
      </PrecompiledHeaderOutputFile>
    </ClCompile>
    

    Then you can instantiate that class like this from your managed C++ code:

    NativeProcessWithThread nativeProcess = NativeProcessWithThread();
    
    0 讨论(0)
提交回复
热议问题