View Full Version : FAQ: How to do simple multi-threading in C/C++? (C++)
sbasak
10-23-2002, 08:34 AM
Hi Everyone
I like to know how I can implement multi-threading in C/C++ programs.
Is it possible to do multi-threading with only one processor?
Can you please show me a SIMPLE C/C++ program which performs multi-threading?
I am using Visual C++ 5 compiler.
Thanks a lot :-)
something like this:
DWORD WINAPI ThreadFunc( LPVOID lpParam )
{
char szMsg[80];
wsprintf( szMsg, "ThreadFunc: Parameter = %d\n", *lpParam );
MessageBox( NULL, szMsg, "Thread created.", MB_OK );
return 0;
}
VOID main( VOID )
{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;
hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
ThreadFunc, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
// Check the return value for success.
if (hThread == NULL)
ErrorExit( "CreateThread failed." );
CloseHandle( hThread );
}
from the win32.hlp..
and please do notice the VOID main() ;)
/btq
sbasak
10-23-2002, 09:05 AM
what files to include, it's giving error even after #include<windows.h>
why VOID anyway? how it differs from void?
you should have
#include <windows.h>
#include <conio.h>
in there, sorry bout that..
/btq
sbasak
10-23-2002, 10:27 AM
In Visual C++ 5 it says
illegal indirection in line wsprintf( szMsg, "ThreadFunc: Parameter = %d\n", *lpParam );
and
ErrorExit -> undeclared identifier
well,for simplicity,just get rid of them then, it's not as if they are the ones makin
the threadin here ;)
just do somethin like:
MessageBox( NULL, "hi I'm the thread", "Thread created.", MB_OK ); instead and exchange the Error-thingie with
MessageBox( NULL, "something wrong with the hThread man!", "error.", MB_OK );
see if that works..:)
/btq
sbasak
10-24-2002, 03:23 AM
now it says linking error
unresolved external symbol _WinMain@16
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.