CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2007
    Posts
    8

    how to create thread in c++

    hai friends
    i want a simple program it must contain only one method. when i call the method it invoke. please give me small thread program in c++ with thread.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: how to create thread in c++

    [ moved thread ]
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    May 2007
    Posts
    437

    Re: how to create thread in c++

    ashu
    always use code tag

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: how to create thread in c++

    You need at least two functions----a main function, and a function to be run in the new thread.

    If you choose to use pthread_create() to make your thread, the function which will run in it must have the prototype
    Code:
    void* threadmain(void *args);
    Where you can make threadmain and args be whatever strings you prefer.

  5. #5
    Join Date
    Jun 2007
    Posts
    21

    Resolved Re: how to create thread in c++

    DWORD WINAPI runThread(LPVOID args)
    {
    int* value = reinterpret_cast<int*>(args);
    printf("\n in thread %d", *value);
    }

    int main()
    {

    DWORD threadId;
    int value = 10;

    hThread = CreateThread( NULL, 0, runThread, &value, 0, &threadId);
    return 0;
    }

    you can use waitforsingleobject() to check the thread status whether is has exited normally or with any error.
    For more details please look into MSDN for each of the API

    Hope this will help you

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: how to create thread in c++

    Check out the Simple Thread: Part I article - it shows how to start, pause, resume, stop and cleanup a thread..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured