multi-threaded programming

This is a discussion on multi-threaded programming within the C++ Programming forums, part of the General Programming Boards category; anyone here know how to do multi-threaded programming in C++? it would be great if someone can show me a ...

  1. #1
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149

    Question multi-threaded programming

    anyone here know how to do multi-threaded programming in C++?
    it would be great if someone can show me a sample.

    here is what i got from a website,but i cannot make it work ....
    Code:
    class Thread
    {
       public:
          Thread();
          int Start(void * arg);
       protected:
          int Run(void * arg);
          static void * EntryPoint(void*);
          virtual void Setup();
          virtual void Execute(void*);
          void * Arg() const {return Arg_;}
          void Arg(void* a){Arg_ = a;}
       private:
          THREADID ThreadId_;
          void * Arg_;
    
    };
    
    Thread::Thread() {}
    
    int Thread::Start(void * arg)
    {
       Arg(arg); // store user data
       int code = thread_create(Thread::EntryPoint, this, & ThreadId_);
       return code;
    }
    
    int Thread::Run(void * arg)
    {
       Setup();
       Execute( arg );
    }
    
    /*static */
    void * Thread::EntryPoint(void * pthis)
    {
       Thread * pt = (Thread*)pthis;
       pthis->Run( Arg() );
    }
    
    virtual void Thread::Setup()
    {
            // Do any setup here
    }
    
    virtual void Thread::Execute(void* arg)
    {
            // Your code goes here
    }
    Last edited by Hermitsky; 11-26-2004 at 03:07 AM.

    blow me ... ...

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Threads are platform-dependent. In Windows, you create them using CreateThread, or beginthread[ex] if you want to use the CRT. In Linux, you have the Linux threads. Using a 2.6 kernel, you instead use the better NPTL for threads. Other Unices may have other libraries, etc.

    There are a few cross-platform thread libraries out there, such as Boost.Threads.
    www.boost.org
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Quote Originally Posted by CornedBee
    Threads are platform-dependent. In Windows, you create them using CreateThread, or beginthread[ex] if you want to use the CRT. In Linux, you have the Linux threads. Using a 2.6 kernel, you instead use the better NPTL for threads. Other Unices may have other libraries, etc.

    There are a few cross-platform thread libraries out there, such as Boost.Threads.
    www.boost.org
    i am programming on windows, would you tell me more details about CreateThread function ? thank you so much

    blow me ... ...

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    _beginthreadex, look it up on MSDN. You just pass a bunch of parameters, one of which is a function pointer, and get back a thread handle. Meanwhile, the other thread starts running. You can use the thread handle to wait for the thread termination, forcefully terminate the thread, retrieve its return code and a few other things.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    i get it, thx man.

    blow me ... ...

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I have a very basic MT tutorial starting here. If you are using the standard library, unless you are sure you have an up to date version of everything, I advise you to use beginthread() and beginthreadex(). CreateThread() has some compatibility issues which can cause memory leaks when it is used with the standard library.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Quote Originally Posted by adrianxw
    I have a very basic MT tutorial starting here. If you are using the standard library, unless you are sure you have an up to date version of everything, I advise you to use beginthread() and beginthreadex(). CreateThread() has some compatibility issues which can cause memory leaks when it is used with the standard library.
    you mean it is dangerous to use CreateThread with C++ standard library,such as ctime and iostream?
    hoho, i wonder if i can finish a multi-threaded program tonight...

    blow me ... ...

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    you mean it is dangerous to use CreateThread with C++ standard library,such as ctime and iostream?
    Yes, exactly that. The C and C++ libraries need some additional housekeeping when starting and ending threads, so you need to use _beginthread[ex] and _endthread[ex], which perform this housekeeping.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  10. #10
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    thanks again, i'll take your advice.
    it is getting more and more complicated...

    blow me ... ...

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > it is getting more and more complicated...
    You have no idea.....

    If you can devise a thread-free solution to your problem, then go for it.

    Without a really strong design, and a solid understanding of the issues, a thread-happy solution will be nothing but a collection of deadlocks and race conditions.
    These are hugely more complicated to debug than a single threaded program.

  12. #12
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Without a really strong design, and a solid understanding of the issues, a thread-happy solution will be nothing but a collection of deadlocks and race conditions.
    From my tutorial...

    Multithreading is about design - it takes no prisoners!
    ... I believed it then, I do today.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  13. #13
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Quote Originally Posted by adrianxw
    From my tutorial...
    what tutorial? if it is about multi-threaded programming, i think i really need it

    blow me ... ...

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    He already gave you a link to his tutorial

  15. #15
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    i am sure there is no link on his post, except i am blind.

    blow me ... ...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help printing a multi array
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 01:35 PM
  2. multi threaded program
    By Dash_Riprock in forum C++ Programming
    Replies: 6
    Last Post: 08-27-2006, 08:38 AM
  3. starting to learn multi threading
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2004, 01:44 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. Signals in multi threaded application
    By HelpMe in forum C Programming
    Replies: 0
    Last Post: 09-12-2001, 09:15 PM

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