![]() |
| | #1 |
| Registered User Join Date: Oct 2002
Posts: 8
| FAQ: How to do simple multi-threading in C/C++? (C++) 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 :-) |
| sbasak is offline |
| | #2 |
| julie lexx... Join Date: Jun 2002
Posts: 161
| something like this: Code: 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 );
}
and please do notice the VOID main() ![]() /btq
__________________ ...viewlexx - julie lexx |
| btq is offline |
| | #3 |
| Registered User Join Date: Oct 2002
Posts: 8
| what files to include, it's giving error even after #include<windows.h> why VOID anyway? how it differs from void? |
| sbasak is offline |
| | #4 |
| julie lexx... Join Date: Jun 2002
Posts: 161
| you should have #include <windows.h> #include <conio.h> in there, sorry bout that.. /btq
__________________ ...viewlexx - julie lexx |
| btq is offline |
| | #5 |
| Registered User Join Date: Oct 2002
Posts: 8
| still error In Visual C++ 5 it says illegal indirection in line wsprintf( szMsg, "ThreadFunc: Parameter = %d\n", *lpParam ); and ErrorExit -> undeclared identifier |
| sbasak is offline |
| | #6 |
| julie lexx... Join Date: Jun 2002
Posts: 161
| 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
__________________ ...viewlexx - julie lexx |
| btq is offline |
| | #7 |
| Registered User Join Date: Oct 2002
Posts: 8
| now it says linking error unresolved external symbol _WinMain@16 |
| sbasak is offline |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Wiki FAQ | dwks | General Discussions | 192 | 04-29-2008 01:17 PM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |
| starting to learn multi threading | hanhao | C++ Programming | 2 | 06-09-2004 01:44 PM |
| Multi Threading | IceBall | C Programming | 7 | 07-13-2003 03:01 PM |
| FAQ Check/Lock | RoD | A Brief History of Cprogramming.com | 2 | 10-15-2002 11:21 AM |