title: How do I check if my multi-threading program works properly in C++?
body: <pre><code>#ifdef _MSC_VER
  #define _CRT_SECURE_NO_WARNINGS
  #endif
  #define MAX_THREADS 1
  
  #include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  
  DWORD WINAPI printNumbe(LPVOID);
  
  // We need an array of Handles to threads
  HANDLE hThreads[MAX_THREADS];
  //...an array of thread id's
  DWORD id[MAX_THREADS];
  //And a waiter 
  DWORD waiter;
  
  DWORD WINAPI printNumber(LPVOID n)
  {
  
  int num = (int)n;
  
  for (int i = 0; i &lt;= num; i++)
  {
      cout &lt;&lt; "Hey there!" &lt;&lt; endl;
  }
  
  return (DWORD)n;
  }
  
  //get ready, because here's where all the REAL magic happens
  int main(int argc, char* argv[])
  {
  
  int number;
  
  cout &lt;&lt; "Please enter a number:" &lt;&lt; endl;
  cin &gt;&gt; number;
  
  //here is where we call the CreateThread Win32 API Function that actually
  //creates and begins execution of thread
  //please read your help files for what each parameter does on
  //your Operating system.
  
  //Here's some basics:
  //Parameter 0: Lookup, 1: Stack Size, 2: The function to run with this   thread, 3: Any parameter that you want to pass to thread
  //function, 4: Lookup , 5: Once thread is created, an id is put in this variable passed in
  
  hThreads[0] = CreateThread(NULL, 0, printNumber, (LPVOID)number, NULL, &amp;id[0]);
  
  //now that all three threads are created and running, we need to stop the primary thread
  // which is this program itself - remember that once "main" returns, our program exits
  //so that our threads have time to finish. To do this, we do what is called "Blocking"
  //we're going to make main just stop and wait until all three threads are done
  //this is done easily with the next line of code. please read the help file about the specific API call
  //"WaitForMultipleObjects"
  
  waiter = WaitForMultipleObjects(MAX_THREADS, hThreads, TRUE, INFINITE);
  
  //after all three threads have finished their task, "main" resumes and we're now ready 
  //to close the handles of the threads. This is just a bit of clean up work.
  //Use the CloseHandle (API) function to do this.
  
  for (int i = 0; i &lt; MAX_THREADS; i++)
  {
      CloseHandle(hThreads[i]);
  }
  
  system("PAUSE");
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>Hi all. Recently I've started taking Operating System class at my college. 
  I got some chance to learn about threads and multi threads. My professor gave me a power point slide with some sample codes on how to start multi-threading program in C++. </p>
  
  <p>So I have decided to use his code as the base and decided to tweak it a bit so that I can understand it better.</p>
  
  <p>Please disregard all those comments I made (most of comments don't apply to this program, those comments were basically in the slide and I just left it there as a reference). </p>
  
  <p>SO I've tweaked to produce "Hey there!" "x" times depends on what number the user inputs into "x". As you are seeing, I made it to print inside printNumber function (sorry about the name, since my main assignment is on printing prime numbers , so forgive me about that). </p>
  
  <p>So the program runs fine and exactly produces "Hey there!" multiple times. </p>
  
  <p>But here's the question. Since my professor wants me to use multi-threads, how do I verify myself in C++ that the program in running with a multi-thread? </p>
  
  <p>This program seems like printing "Hey there!" in a row (just like under a single thread) and there's no way I can tell if multi-threading has been applied to the program or not. </p>
  
  <p>Keep in mind that, I am not that familiar with those syntax and it's my first time using WINAPI in C++ as well as first time doing threading programming. </p>
  
  <p>Thank you very much. </p>