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*);
};
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.