C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 10-23-2002, 07:34 AM   #1
Registered User
 
sbasak's Avatar
 
Join Date: Oct 2002
Posts: 8
FAQ: How to do simple multi-threading in C/C++? (C++)

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 :-)
sbasak is offline  
Old 10-23-2002, 07:41 AM   #2
btq
julie lexx...
 
btq's Avatar
 
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 );
}
from the win32.hlp..
and please do notice the VOID main()

/btq
__________________
...viewlexx - julie lexx
btq is offline  
Old 10-23-2002, 08:05 AM   #3
Registered User
 
sbasak's Avatar
 
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  
Old 10-23-2002, 09:08 AM   #4
btq
julie lexx...
 
btq's Avatar
 
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  
Old 10-23-2002, 09:27 AM   #5
Registered User
 
sbasak's Avatar
 
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  
Old 10-23-2002, 09:56 AM   #6
btq
julie lexx...
 
btq's Avatar
 
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  
Old 10-24-2002, 02:23 AM   #7
Registered User
 
sbasak's Avatar
 
Join Date: Oct 2002
Posts: 8
now it says linking error

unresolved external symbol _WinMain@16
sbasak is offline  
 

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:19 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22