How to do simple multi-threading in C/C++?

This is a discussion on How to do simple multi-threading in C/C++? within the C++ Programming forums, part of the General Programming Boards category; Hi Everyone I like to know how I can implement multi-threading in C/C++ programs. Is it possible to do multi-threading ...

  1. #1
    Registered User sbasak's Avatar
    Join Date
    Oct 2002
    Posts
    8

    How to do simple multi-threading in 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
    btq
    btq is offline
    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
    btq
    btq is offline
    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
    btq
    btq is offline
    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. 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
  2. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  3. Help me with these simple programs
    By Help me in forum C Programming
    Replies: 4
    Last Post: 11-08-2001, 09:38 AM
  4. Need help with simple data types
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 08:36 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM

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