Can use std::thread with c++/clr?

Hello can somehow be used thread or its replacement in c ++ / clr?
The short answer is no.

CLR is a virtual machine, which introduces a different can of worms to be aware of. Here is some reading to get you started. https://docs.microsoft.com/en-us/archive/msdn-magazine/2008/december/clr-inside-out-thread-management-in-the-clr
Hm... I don't know... I think as long as you stick to pure native code you should be alright. I might try this myself, actually.
Conclusions:

1. Including <thread> gives the error "<thread> is not supported when compiling with /clr or /clr:pure". It may be possible to force the compiler to accept the header by modifying it, but I'm not gonna try that.
2. Using Windows functions for thread creation and synchronization and sticking to native code in the second thread doesn't crash:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "stdafx.h"

using namespace System;

class Mutex{
    CRITICAL_SECTION cs;
public:
    Mutex(){
        InitializeCriticalSection(&this->cs);
    }
    ~Mutex(){
        DeleteCriticalSection(&this->cs);
    }
    void lock(){
        EnterCriticalSection(&this->cs);
    }
    void unlock(){
        LeaveCriticalSection(&this->cs);
    }
};

class AutoMutex{
    Mutex *m;
public:
    AutoMutex(Mutex &m): m(&m){
        this->m->lock();
    }
    ~AutoMutex(){
        this->m->unlock();
    }
};

Mutex cout_mutex;
#define LOCK_MUTEX(x) AutoMutex lg(x)

DWORD WINAPI thread_function(LPVOID){
    while (true){
        {
            LOCK_MUTEX(cout_mutex);
            std::cout << "Hello from non-main thread.\n";
        }
        Sleep(1000);
    }
    return 0;
}

int main(array<System::String ^> ^args){
    auto thread = CreateThread(nullptr, 0, thread_function, nullptr, 0, nullptr);
    while (true){
        {
            LOCK_MUTEX(cout_mutex);
            Console::WriteLine(L"Hello from main thread.");
        }
        Sleep(1100);
    }
    WaitForSingleObject(thread, INFINITE);
    CloseHandle(thread);
    return 0;
}
3. Replacing the iostream call in thread_function() with a Console::WriteLine() call still doesn't crash.
4. Making a more complex call (I tried System::Windows::Forms::MessageBox::Show()) from thread_function() still works correctly.
5. Last example. Asynchronously modifying data from a separate thread:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
ref class StringWrapper{
public:
    System::String ^s;
};

DWORD WINAPI thread_function(LPVOID ptr){
    auto handle = System::Runtime::InteropServices::GCHandle::FromIntPtr(IntPtr(ptr));
    auto display_string = (StringWrapper ^)handle.Target;
    int i = 0;
    while (true){
        {
            LOCK_MUTEX(cout_mutex);
            std::cout << "Hello from non-main thread.\n";
            display_string->s = System::Convert::ToString(i++);
        }
        Sleep(1000);
    }
    return 0;
}

int main(array<System::String ^> ^args){
    auto display_string = gcnew StringWrapper();
    auto handle = System::Runtime::InteropServices::GCHandle::Alloc(display_string);
    auto thread = CreateThread(nullptr, 0, thread_function, handle.ToIntPtr(handle).ToPointer(), 0, nullptr);
    while (true){
        {
            LOCK_MUTEX(cout_mutex);
            Console::WriteLine(L"Hello from main thread. " + display_string->s);
        }
        Sleep(1100);
    }
    WaitForSingleObject(thread, INFINITE);
    CloseHandle(thread);
    return 0;
}
Last edited on
Topic archived. No new replies allowed.