Instructables

Easy Way to Use Threads in Visual C++ (VC++)

a bare minimum of what you need to use threads in VC++. Threads allow your program to do multiple things at the same time (multithreading) instead of in a linear sequence. For instance, you could have your program doing an infinately looped complex task and at the same time be waiting for the user to enter a 'stop' key. Using multithreading, the user could press the 'stop' key at _any_ time to stop the program.
 
Remove these adsRemove these ads by Signing Up

Step 1: #including the propper stuff

even if you have already started an app as a non MFC console application, you can use threads in your app.

1) First, go into setting and set it so that it uses MFC as a static library

2) Then add #include below the #include "stdafx" that should already be in your program

3) meanwhile, if you have
#include "windows.h" or #include "mmsystem.h"
put these two _above_ the #include "stdafx.h"

4) Be careful, the order in which you declare the #includes matters a lot!! (if the way you have it gives errors during compile, reorder them til it works).

Step 2: Write The Necessary Code

Below is all the code you need to add threads to your project.

UINT LeesThread( LPVOID pParam )
is the thread, put your thread code in there (it's like a function)

AfxBeginThread(LeesThread, TempChar);
is the code that starts your thread, in this example it's at the start of the main() function.

TempChar
is just a character pointer that needs to be sent to the thread for some reason, the value of it doesn't matter.

Note: This has worked fine for me in many programs, everywhere I read about how to make threads has been pretty complicated, so although this will work, it's probably not technically correct. I don't care, it works:

//#############################################
//############---CODE BELOW---#################
//#############################################

#include

char* TempChar;


UINT LeesThread( LPVOID pParam )
{
//--put your thread code here
//--use a while(TRUE) loop if you want it to run continuously
return 0;
}

void main()
{
//--Start your thread (in this case, LeesThread)
AfxBeginThread(LeesThread, TempChar);
}
ThatGuy8 years ago
Your "TempChar" is actually a pointer to void (or void *). This can carry anything to your thread, it could be nothing as you suggest, or a pointer to an XML string, or an array of structures... anything. This parameter is there so that you can communicate to your thread, it could be data that it should operate on or information about how to get to syncronization objects so that the rest of your program can signal it, or anything else that you need to provide to the thread for its execution. The TempChar is what is passed to your thread in that LPVOID pParam (LPVOID is the same as void *).
leevonk (author)  ThatGuy8 years ago
ohhhh, cool. I just communicate with the thread via global variables :P I don't write huge commercially viable programs, just smallish ones for research, so don't yell at me for using globals :)