Thread: Threading

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Threading

    Please help me. I need to know how to write a simple multithreaded program. Thank you

  2. #2
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Geez, ever heard of a search feature?

    Or do you want a complete spoon-fed answer because you're too helpless to do any actual work yourself?

    Nevermind - don't bother answering unless you have something useful to say....

    Bah

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    :(

    This can be more difficult than you might think. And yes, I have searched, and instead of posting that mean reply, you could have given me a link at least, Thanks anyway

  4. #4
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    this is a sample, can't remember where i got it -_-"
    it use _beginthread() function which is included in C run-time libraries. you can get more details on msdn.
    Code:
    #include <windows.h>
    #include <process.h>
    #include <iostream>
    using namespace std;
    
    void Func1(void *);
    void Func2(void *);
    
    CRITICAL_SECTION Section;
    
    int main()
    {
        InitializeCriticalSection(&Section);
    
        _beginthread(Func1,
                     0,
                     NULL);
        _beginthread(Func2,
                     0,
                     NULL);
    
        Sleep(10000);
    
        DeleteCriticalSection(&Section);
    
        cout << "Main exit" << endl;
    
        return 0;
    }
    
    void Func1(void *P)
    {
        int Count;
    
        for (Count = 1; Count < 11; Count++)
        {
            EnterCriticalSection(&Section);
            cout << "Func1 loop " << Count << endl;
            LeaveCriticalSection(&Section);
        }
        return;
    }
    
    void Func2(void *P)
    {
        int Count;
    
        for (Count = 10; Count > 0; Count--)
        {
            EnterCriticalSection(&Section);
            cout << "Func2 loop " << Count << endl;
            LeaveCriticalSection(&Section);
        }
        return;
    }
    
    
    
    
    int main()
    {
        HANDLE hThreads[2];
    
        InitializeCriticalSection(&Section);
    
        hThreads[0] = (HANDLE)_beginthread(Func1,
                                           0,
                                           NULL);
    
        hThreads[1] = (HANDLE)_beginthread(Func2,
                                           0,
                                           NULL);
    
        WaitForMultipleObjects(2,
                               hThreads,
                               TRUE,
                               INFINITE);
    
        DeleteCriticalSection(&Section);
    
        cout << "Main exit" << endl;
    
        return 0;
    }

    blow me ... ...

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Thank you very much. I'll try and see.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Doesn't work

    Ok I tried. But it says _beginthread can't be found. And I am using visual studio 2003. I also tried the code on digital mars. And anyway, why are there two declarations of main()? thank you in advance

  7. #7
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    It looks like a mistake. They are just two different ways to handle things. The first main() is sleeping for 10 seconds and assuming that both threads have exited by the time the wait expires. The second main() is getting a handle to each thread, and explicitly waiting for both threads to exit before continuing. Take your pick and comment the other one out. As far as the linker not finding _beginthread, you need to compile with /MT. Since you have visual studio 2003, why not click on help->index, type in _beginthread, and look at the example code that they give you for free. Just a thought.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Smile

    Yup, exactly what I did. Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ threading
    By Anddos in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:29 PM
  2. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  3. Problem with threading
    By osal in forum Windows Programming
    Replies: 6
    Last Post: 07-21-2004, 12:41 PM
  4. starting to learn multi threading
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2004, 01:44 PM
  5. Threading
    By threads in forum C# Programming
    Replies: 0
    Last Post: 01-17-2003, 11:50 PM