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

    How to decide which type of thread to use?

    There are gnu thread, c thread, posix thread and win32 thread
    which all can be used on windows platform.
    How, why and when do I need them?
    Code:
    #ifndef C_THREAD_H_
    #define C_THREAD_H_
    
    extern "C"
    {
    #include <cthread.h>
    }
    
    #ifndef BOOLEAN_H_
    #  include <Common/Boolean.h>
    #endif
    
    class Thread;
    
    /*
     * General notes: the C Threads library does not have any notion of priorities.
     */
    
    /*
     * This is a threads interface to the C threads library.
     */
    
    class ThreadData
    {
    public:
        ThreadData ();
        ~ThreadData ();
        
        cthread_t cid;
        mutex_t   mx;
        Boolean   dead;
    
        static void* Execute (void* p1);
        
        static long base_key;
        static Thread* mainThread;
        static long mainThreadID;
    };
    
    class C_Mutex : public Mutex
    {
    public:
        C_Mutex ();
        ~C_Mutex ();
    
        Boolean lock ();
        Boolean unlock ();
    
    private:
        mutex_t _theLock;
    };
    
    #endif
    This simply won't compile with Visual C++ because there isn't any cthread.h header file there.
    Thanks
    Jack
    Last edited by lucky6969b; June 7th, 2014 at 09:14 PM.

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

    Re: How to decide which type of thread to use?

    On Windows, I use:

    _beginthreadex and QueueUserWorkItem.

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    Re: How to decide which type of thread to use?

    Do I need to make up a variable in order to "hold" the thread?
    I notice that _beginthreadex just returns a handle.

    http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx

    Thanks
    Jack

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

    Re: How to decide which type of thread to use?

    Quote Originally Posted by lucky6969b View Post
    Do I need to make up a variable in order to "hold" the thread?
    I notice that _beginthreadex just returns a handle.

    http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx

    Thanks
    Jack
    Yes, The link you posted contains an example of _beginthreadex. Also check out QueueUserWorkItem - it's a thread pool mechanism.

  5. #5
    Join Date
    Jul 2013
    Posts
    576

    Re: How to decide which type of thread to use?

    Quote Originally Posted by lucky6969b View Post
    There are gnu thread, c thread, posix thread and win32 thread
    which all can be used on windows platform.
    How, why and when do I need them?
    And then there are the C++ 11 threads. Concurrency is now part of standard C++,

    http://www.amazon.com/gp/product/193...pf_rd_i=507846

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

    Re: How to decide which type of thread to use?

    Quote Originally Posted by razzle View Post
    And then there are the C++ 11 threads. Concurrency is now part of standard C++,

    http://www.amazon.com/gp/product/193...pf_rd_i=507846
    That would definitely be a better way to go if you are using C++ 11 or better.

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