Createthread()blocks the mainthread when i use RAII.

Here is my code.if you run this,you will recv "?" full with the screen,and you can't recv"hi"cause the mainthread was blocked.I want to know the why and the how to avoid this.

#include<Windows.h>
#include<iostream>
using namespace std;
class test
{
private:
HANDLE _thread;
public:
test(){
_thread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)thread_1(this), NULL, NULL, NULL);
}
friend DWORD WINAPI thread_1(test*);
};

DWORD WINAPI thread_1(test*t)
{
while (1){
cout << "?";
}
return 1;
}

int main()
{
test t1;
cout << "hi" << endl;
return 0;
}
Last edited on
You're not passing the function as a function pointer, you're simply calling the function before the call to CreateThread() happens. Also, the function is incorrectly defined. The parameter should be an LPVOID. You need to pass the this pointer as the fourth parameter to CreateThread(). Once the new thread has started, the system will pass this pointer to the callback.
tks very much. It's very useful.
Topic archived. No new replies allowed.