C++ Threads

C++ Threads are useful for doing more than one thing at the same time, or have a loop do your work while the rest of the program continues to operate at the same exact time.

First off we need to include <windows.h> and <stdio.h>. These provide us with the basic functions need for threading, and stdio.h provides us with very basic functions useful for most applications.

toc_collapse=0;
Contents 

Creating the thread

Now, we must create the thread. First off, declare the thread. I'm going to call mine threader, you can name your thread whatever you like. (Hint: Declarations go outside of functions)
DWORD WINAPI Threader( LPVOID lpData );
That is what I always use for my threads, then I just change the name to fit my needs.

Remember to include in your files.

So now make the function for the thread. You do this by copying the declaration we just made, and replacing the ; with { signaling that we are going to place some code there.

Now throw some code in there for what you want it to do. (Hint: Use a loop, or some type of delay, otherwise threading is basically pointless.) I used:

DWORD WINAPI Threader( LPVOID lpData )
{
      int i = 0;
      while(1)
      {
               printf("%u ", i);
               i = rand();
               Sleep(500);
      }
}

Now, we have to call the thread so it actually does something. To do this we need to create a handle, then call the function CreateThread.
From MSDN

HANDLE WINAPI CreateThread(
  __in_opt   LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in       SIZE_T dwStackSize,
  __in       LPTHREAD_START_ROUTINE lpStartAddress, << Only one you need to worry about.
  __in_opt   LPVOID lpParameter,
  __in       DWORD dwCreationFlags,
  __out_opt  LPDWORD lpThreadId
);

To create a handle just simply type (Within int main function), HANDLE *Handle Name*. For the sake of the tutorial I'll just call mine thread.
Now, after HANDLE Thread make an = sign and type CreateThread(Null, 0, Threader, Null, 0, 0);

Closing the thread

To close a thread simply call the function CloseHandle(*handleNameHere*)

You should have this:
CloseHandle(Thread);

Overall this is what I end up with:

#include <windows.h>
#include <stdio.h>

using namespace std;

DWORD WINAPI Threader( LPVOID lpData );
int CloseThread;

int main(int argc, char *argv[])
{
    HANDLE Thread = CreateThread(NULL, 0, Threader, NULL, 0, 0);
    Sleep(5000);
    printf("This should show up in the middle of numbers. ");
    getchar();
    CloseHandle(Thread);
    return 0;
}

DWORD WINAPI Threader( LPVOID lpData )
{
      int i = 0;
      while(1)
      {
               printf("%u ", i);
               i = rand();
               Sleep(500);
      }
}

Anonymous's picture

Is there any maximum amount

Is there any maximum amount of threads i can run simultaneously?

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.