Results 1 to 3 of 3

Thread: Calling same method from separate threads

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Calling same method from separate threads

    Hi,

    I have a function called GetMessageDetail which returns a pointer to a structure. This method is called from two separate threads, the function is as follows :

    Qt Code:
    1. PASSTHRU_MSG* QTCanMonitor::GetMessageDetail( unsigned long *pulNumMsgs, int iChannelIndex )
    2. {
    3. unsigned long ulTimeout = 0;
    4. int iReply = -1;
    5.  
    6. if( m_pPassThruReadMsgs && m_bConnected )
    7. {
    8. iReply = (m_pPassThruReadMsgs)( m_lChannelID[iChannelIndex], m_CanMsg, pulNumMsgs, ulTimeout );
    9. if( iReply == -1 )
    10. {
    11. return NULL;
    12. }
    13. }
    14. else
    15. return NULL;
    16. return m_CanMsg;
    17. }
    To copy to clipboard, switch view to plain text mode 

    What would be the best way to make sure this method is not accessed by the two threads at the same time? Would a QMutex be ok?

    Thanks,
    Steve

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling same method from separate threads

    Yes, but not a function scope mutex, but a class scope one.
    The threads are accessing this method within the same instance of the class, right?

    If they operate on two different instances, then there is no problem, unless you have static members/data that the function modifies.

    Regards

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Calling same method from separate threads

    Thanks Marcel,

    I have put a QMutex in my class and have done the following :

    Qt Code:
    1. PASSTHRU_MSG* QTCanMonitor::GetMessageDetail( unsigned long *pulNumMsgs, int iChannelIndex )
    2. {
    3. QMutexLocker locker(&m_mutex);
    4.  
    5. unsigned long ulTimeout = 0;
    6. int iReply = -1;
    7.  
    8. if( m_pPassThruReadMsgs && m_bConnected )
    9. {
    10. iReply = (m_pPassThruReadMsgs)( m_lChannelID[iChannelIndex], m_CanMsg, pulNumMsgs, ulTimeout );
    11. if( iReply == -1 )
    12. {
    13. return NULL;
    14. }
    15. }
    16. else
    17. return NULL;
    18. return m_CanMsg;
    19. }
    To copy to clipboard, switch view to plain text mode 

    Where m_mutex is the QMutex at class scope.

    This works a treat. And yes, there is only one instance of the class the above method is in.

    Regards,
    Steve

Similar Threads

  1. Calling Recursivly loading function in Run() method of QThread
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 14:42
  2. Replies: 4
    Last Post: 10th March 2007, 18:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.