title: PeekMessage inside thread
body: <p>I'm a bit confused. What I'm trying to do - is to create <em>a window messages loop inside a different thread</em>. My code looks like this:</p>
  
  <pre><code>//...
  #include &lt;thread&gt;
  //...
  
  void MyClass::runMainLoop() {
      new thread(mainLoop, this); //I know this will cause a memory leak - just for testing
  }
  
  void MyClass::mainLoop(MyClass* _this) { // &lt;- static method
      cout &lt;&lt; "start thread" &lt;&lt; endl; //loop function started
      MSG msg;
      while (true) {
          while (PeekMessage(&amp;msg, _this-&gt;_hWnd, 0, 0, PM_REMOVE)) {
              cout &lt;&lt; msg.message &lt;&lt; " "; //we've got a message! (don't get here inside a thread)
              if (msg.message == WM_QUIT) {
                  cout &lt;&lt; "exiting" &lt;&lt; endl; //closing window
                  break;
              }
              DispatchMessage(&amp;msg);
          }
  
          Sleep(2);
      }
  }
  </code></pre>
  
  <p>The message "start thread" appears, but I don't see that any messages where handled. On the other hand, when I call to mainLoop() method without creating a thread, everything seems to work fine:</p>
  
  <pre><code>void MyClass::runMainLoop() {
      mainLoop(this);
  }
  </code></pre>
  
  <p>I tryed to dig into MSDN, but didn't find anything on my problem. It seems, that I have some gaps in my knowledge, which can't be filled in a reasonable time.</p>
  
  <p>My thought was that the thread somehow "don't know" about a window I've created inside my main program thread.</p>
  
  <p>So, the question is - what am I doing wrong? Why the message loop isn't working in thread?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The code which creates the window. It runs in the program's main thread.</p>
  
  <pre><code>WNDCLASSEX            wcx;
  PIXELFORMATDESCRIPTOR pfd;
  RECT                  rect;
  HGLRC                 hRCTemp;
  DWORD                 style, exStyle;
  int                   x, y, format;
  
  _hInstance = (HINSTANCE)GetModuleHandle(NULL);
  
  //Register window class
  memset(&amp;wcx, 0, sizeof(wcx));
  wcx.cbSize        = sizeof(wcx);
  wcx.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  wcx.lpfnWndProc   = (WNDPROC)windowProc;
  wcx.hInstance     = _hInstance;
  wcx.lpszClassName = L"windowClassName";
  wcx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  wcx.hCursor       = LoadCursor(NULL, IDC_ARROW);
  
  RegisterClassEx(&amp;wcx)
  
  //Window styles
  style   = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
  exStyle = WS_EX_APPWINDOW;
  
  //place window at the cetner of the screen
  x = (GetSystemMetrics(SM_CXSCREEN) - width)  / 2;
  y = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
  
  rect.left   = x;
  rect.right  = x + width;
  rect.top    = y;
  rect.bottom = y + height;
  
  //Adjust  window size to styles
  AdjustWindowRectEx(&amp;rect, style, FALSE, exStyle);
  
  //Create a window
  _hWnd = CreateWindowEx(exStyle, wcx.lpszClassName, L"Window caption", style, rect.left, rect.top,
      rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, _hInstance, NULL);
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Thanks to all those who commented my question, I understand now where the problem is: <em>the thread that created the window receives the messages for it</em>.</p>
  
  <p>So, I'll rephrase the question: <strong>can I redirect window messages to another thread</strong>? I tried <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms681956%28v=vs.85%29.aspx" rel="nofollow">AttachThreadInput</a>, but didn't succeed.</p>