Thread: FAQ: How to do simple multi-threading in C/C++? (C++)

  1. #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 :-)

  2. #2
    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

  3. #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?

  4. #4
    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

  5. #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

  6. #6
    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

  7. #7
    Registered User sbasak's Avatar
    Join Date
    Oct 2002
    Posts
    8
    now it says linking error

    unresolved external symbol _WinMain@16

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. starting to learn multi threading
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2004, 01:44 PM
  4. Multi Threading
    By IceBall in forum C Programming
    Replies: 7
    Last Post: 07-13-2003, 03:01 PM
  5. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 11:21 AM