Using clr and std :: thread

I am creating a desktop UI abstraction layer. Now I am implementing the functionality of the .NET framework. It's a shame that if I let users create a Visual CRE for CLR application in Visual Studio, they won't be able to use all the standard libraries like std::thread

, and if I let them create another type of application, the console appears.

Is there a way to use clr with, std::thread

or better yet, is there a way to prevent the console from starting (or hide it from both the screen and the taskbar) using the CLR Console or CLR Blank Project.

thank

0


source to share


2 answers


This is an old question, but in case anyone is running into the same problem: boost::thread

is it an "affordable" and practical replacement (assuming you can use boost in your project). Ironically, it bypasses the incompatibility.



0


source


Maybe an old question, but I've looked into this issue before. Since the CLR doesn't allow std::thead

compile-time inclusion , you can try to use it only at link time. You can usually allow this to declare the class in your header and only include them in your cpp files. However, you can redirect your own classes to 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 streaming class that inherits from std::thread

, which you can stream . The header file 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 the stream you can do, declare it like this:



#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