Code:.:[Smith's guide to Multi-Threaded Programming in C++]:. .:I make programming FUN!!:. .:[Part 1]:. OK, before we jump into this all unprepared we need to go over a few things first! What you need: 1. Compiler ( I use Microsoft Visual Compiler 6.0 ) 2. Win32 API Reference ( Don't have it? use google ) 3. A Brain ( Preferably in working order ) Ok lets begin with a basic C++ layout for Win32, it should look like this:----------------------------------- Now if we look at our Win32 API Ref and look up CreateThread we get this:Code:------------------------------------ #include <stdio.h> #include <windows.h> int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { return(0); }This will get explained later. I just have it here for reference. Ok onto the code now we need to add a few things first:HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes DWORD dwStackSize, // initial thread stack size, in bytes LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function LPVOID lpParameter, // argument for new thread DWORD dwCreationFlags, // creation flags LPDWORD lpThreadId // pointer to returned thread identifier );Ok, time to explain how this all works. Ok this line here: ULONG WINAPI OurFirstThread(LPVOID); Sets the thread up. Kinda like Dim <value> in VB. I wont go into a huge explaination you just need it ok? This part here: ULONG WINAPI OurFirstThread(LPVOID p) { MessageBox(NULL,"Yay our thread was excuted","Smith's Multi-Thread Example",MB_OK); return(0); } This is the part of the program that gets turned into a thread. Whatever code is between the "{" and "}" will be executed seperatly from the rest of the program. And finally we get to the actual creation of the thread: CreateThread(NULL,0,OurFirstThread,0,0,&ThreadId); This creates the thread the functions are explained in order. 1. Do we want a child processes to be able to inherit this thread? No 2. How big in bytes will this thread be? 0 = Autosize 3. Thread Name 4. Useless Parameter leave as 0 5. Creation flag, we can have different values in here, say you want make a thread but have it paused to run at another time. You would use the CREATE_SUSPENDED flag. 6. Finally the pointer to hold the data ThreadId, yes it is needed. *Just remember this is a basic tutorial. Finally after all this you may be wondering, this is all good and well WHY? Would i need this? Well threads are mainly used when a program needs to do 2 things at the same time. Because your program can't do to things at once can it? Not unless you use threads. Example: Looping and Creating a socket.Code:#include <stdio.h> #include <windows.h> ULONG WINAPI OurFirstThread(LPVOID); ULONG WINAPI OurFirstThread(LPVOID p) { MessageBox(NULL,"Yay our thread was excuted","Smith's Multi-Thread Example",MB_OK); return(0); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { unsigned long ThreadId; CreateThread(NULL,0,OurFirstThread,0,0,&ThreadId); Sleep(5000) \\ Sleep for 5 seconds so the MB can be \\ displayed. return(0); }
Results 1 to 1 of 1
- 01 Jul. 2010 04:45pm #1
[C++]Programming in general with multiple threads.