How can I combine Concurrency runtime with .NET code?

I used Concurrency Runtime in a C ++ static library and recently wanted to use this library in C ++ / CLI to use the Windows Form constructor and avoid MFC. Unfortunately, the lifetime of Concurrency is incompatible with the / clr switch necessary for C ++ / CLI. I tried to surround the included header files that use the Concurrency Runtime in the #pragma unmanaged ... #pragma managed directives, but so far this has worked for me with other code in the past, it does not seem to work in This case. By this I mean that I get an error:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\concrt.h(27): fatal error C1189: #error : ERROR: Concurrency Runtime is not supported when compiling /clr. 

I'm not very good at mixing managed and unmanaged code, so itโ€™s possible that there is a workaround that I donโ€™t know about. But, on the other hand, perhaps this is just a stupid approach. If it were not for the fact that I find MFC incredibly complex, and the form designer is so nice and easy, I would just make pure C ++. Preferred to mix these two sentences?

+4
source share
5 answers

Using ConcRT in C ++ / CLI is explicitly disabled in concrt.h using the instructions 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 get around this, as suggested above, or you can also use the pointer to the implementation idiom to address this by redirecting the "pimpl" class and hide the dependency on concrt.h to your own .cpp file which you can then compile in lib and the link against the header file.

eg. in the .h file:

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

eg. in your .cpp file that compiles to your own lib:

  #include <ppl.h> class PImpl { //some concrt class Concurrency::task_group m_tasks; } 
+7
source

You might want to write a managed graphical interface and call it (using PInvoke) an unmanaged DLL: if you can package the Concurrency Runtime and the code that uses it as a DLL.

0
source

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

 array<MyModelResult^>^ model ....; #pragma omp parallel for for(int i=0;i<model->Length;i++) { model[i] = ComputeModelFor(i); } 
0
source

Even if ConcRT in C ++ / CLI is explicitly disabled, you can compile your project with clr support and have some native classes in one project by setting the CompileAsManaged property to false and PrecompildHeader in NotUsing in your vcxproj file (I've checked 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 create an instance of this class like this from your managed C ++ code:

 NativeProcessWithThread nativeProcess = NativeProcessWithThread(); 
0
source

I ran into the same problem when I linked C ++ to C # using the CLR. This problem occurred when directly referencing the following items in the header file (*. H) that was included in the CLR project.

 #include <ppl.h> using namespace concurrency; 

Moving these two lines to the corresponding * .cpp file solved the problem.

0
source

All Articles