Results 1 to 16 of 16

Thread: No context switch between Threads and parent ?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2008
    Posts
    66
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default No context switch between Threads and parent ?

    Hi,

    The following situation is given:

    There is an Object Instance of a Serial Class , which has the abilitiy to read() and write() on com-port. The Serial Class itself has in its privates an instance of a communication library I use.
    The read() write() methods above use methods of this com. library.

    Qt Code:
    1. class Serial:
    2.  
    3. Serial()
    4. {
    5. ...
    6. reader = new threads(this,true);
    7. writer = new threads(this,false);
    8.  
    9. reader->start();
    10. writer->start();
    11. ...
    12. }
    13.  
    14. read()
    15. {
    16. //use here the com library function and some winapi functions to read from the com port
    17. }
    18.  
    19. write()
    20. {
    21. // same as above but or writing
    22. }
    23.  
    24. private:
    25. Seriallibrary Slib; // instance of foreign serial library
    26. Thread-Class *reader,*writer;
    To copy to clipboard, switch view to plain text mode 

    What I wanted to do is following:
    Create two threads, one for reading operations and one for writing operations. (asynchron communication)
    They had to use the methods I provide with my Serial Class.

    What I have done is:

    I created a "Thread-Class" derived from QThread.
    I put two pointers of Type "Thread-Class" to my Serial Class (private member).
    The Ctor of the "Thread-Class" has a boolean, to decide if it is a reader or a writer thread.
    In dependency of that boolean one of two possible loops will be started.

    These two loops use the Serial Class functions provided for writing or reading (with pointer serialPTR).
    I think the whole stuff I designed is wrong. Is every function call I make from the threads running in the context of my Serial Class-Instance ????

    Qt Code:
    1. Threads:
    2.  
    3. Threads(parent *p,bool typeofThread)
    4. {
    5. //take the parent pointer (serialPTR), because I need it to call the provided functions for reading and writing
    6.  
    7. //set a local flag if I am a Reader Thread or a Writer Thread.
    8. }
    9.  
    10.  
    11. run()
    12. {
    13. if(readerFlagIsSet)
    14. while(!threadDone)
    15. {
    16. serialPTR->read();
    17. }
    18.  
    19. if(writerFlagIsSet)
    20. while(!threadDone)
    21. {
    22. serialPTR->write();
    23. }
    24.  
    25. }
    26. // I mean the two function calls above from the thread to the serial-Class
    To copy to clipboard, switch view to plain text mode 

    Can you give me a hint to make that better?
    Or is this way ok?
    Last edited by donglebob; 24th October 2008 at 14:00.

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
  •  
Qt is a trademark of The Qt Company.