Results 1 to 7 of 7

Thread: multithreading simple question

  1. #1
    Join Date
    Jan 2012
    Posts
    20
    Thanks
    3

    Default multithreading simple question

    Hi all,

    As the forum name suggests i am a newbie to qt programming and this the first time i am posting and hope to get some reply. i have one of these tasks to perform i have one PCI based card with some digital inputs and outputs i have to read the status of those inputs continuously (safely i can read at 100ms without loss of data the lesser it is still better). For this task the supplier has provided a function waitForIRQ() and explains that it would deadlock the thread and returns non zero if some irq happened and returns 0 if some error happened. Now i am trying to write the program in qt and created a thread like this

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. my_thread thread;
    6. thread.start();
    7. // doing some functionality in the main
    8. return app.exec();
    9. }
    10.  
    11. void my_thread::run()
    12. {
    13. while( waitForIRQ())
    14. return value;
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    i am sorry i have just started QT and C++. please let me know if i have written the code properly. sorry for the poor explanation.

    please let me know if i did not explain clearly.

    thanks and regards,
    satya
    Last edited by wysota; 31st January 2012 at 10:39. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: multithreading simple question

    It's more or less okay (provided you do assign some values to "value") however it would be better to do it without having to block the thread. Does the supplier provide any non-blocking API as well?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Posts
    20
    Thanks
    3

    Default Re: multithreading simple question

    Thank you for the reply. i will try to execute the program and see if i could run the program successfully. one thing is i did not understand this blocking and non blocking api you have mentioned i did not think of that. if you don't mind could you please explain me. i will also try google to understand.

    thanks and regards,
    satya

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: multithreading simple question

    Blocking API is what you called "deadlock" (it's not actually a deadlock because a deadlock situation is that something is waiting for an event that will never happen) which means that when you call a function, it blocks execution of the program until there is data available. Non-blocking API on the other hand would return immediately telling you there is no data to be read so that you can attend other tasks before data is available. This is usually accompanied by some mechanism (like a unix signal, interrrupt, socket listener) that will notify you that data is available and you can read it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2012
    Posts
    20
    Thanks
    3

    Default Re: multithreading simple question

    Hi,

    i still have some problems of basic understanding of the threading currently i am showing the pseudo code of what i have written
    Qt Code:
    1. /* main.h */
    2.  
    3. class test:public QObject
    4. {
    5.  
    6. Q_Object
    7.  
    8. public:
    9. readinputstatuschangeLedColourtoGreen();
    10. maketheflagTrue();
    11.  
    12. }
    13.  
    14.  
    15. class mythread:public Thread
    16. {
    17. Q_Object
    18.  
    19. public:
    20. mythread();
    21.  
    22. protected:
    23. void run();
    24.  
    25. public:
    26. int my_flag;
    27.  
    28. public slots:
    29. void maketheflagTrue();
    30. }
    31.  
    32.  
    33.  
    34. /* main.cpp*/
    35.  
    36. int main(int argc, char *argv[])
    37. {
    38. QApplication app(argc,argv);
    39.  
    40. Window window;
    41.  
    42.  
    43. mythread threadA;
    44.  
    45. threadA.start();
    46.  
    47. window.show();
    48.  
    49. return app.exec();
    50.  
    51. }
    52.  
    53.  
    54. /* file1.cpp */
    55.  
    56. test test1; // created object of test
    57.  
    58. window::Window
    59. {
    60.  
    61. // created a gui with all the switches and Leds (switches are normal push button switches and leds are
    62. // normal circles where you can fill with either green or red colour depending on the input data from
    63. // pci based input card.
    64.  
    65. QPushButton *button1 = new QPushButton("BUTTON1");
    66.  
    67. QObject::connection(button1, SIGNAL(clicked()), &test1, SLOT(maketheflagTrue()));
    68.  
    69. }
    70.  
    71. /* main.cpp*/
    72.  
    73. mythread::mythread()
    74. {
    75. my_flag = FALSE;
    76. }
    77.  
    78. mythread::run()
    79. {
    80. while(!myflag)
    81. {
    82. }
    83. readinputstatuschangeLedColourtoGreen();
    84. my_flag = FALSE;
    85. }
    86.  
    87.  
    88. /*file3.c*/
    89.  
    90. extern test test1;
    91.  
    92. void test:: readinputstatuschangeLedColourtoGreen()
    93. {
    94. // based on the input data i would make the leds green or red if input is 1 or 0 respectively.
    95. }
    96.  
    97. void test::maketheflagTrue()
    98. {
    99. test1.my_flag = TRUE;
    100. }
    To copy to clipboard, switch view to plain text mode 

    i will very quickly and briefly explain the code
    1. i have to actually get the data from the pci card which currently i am trying to simulate with button1 and the flag i am using my_flag.
    2. my main purpose is read some input data and color the led which i am trying to do it in thread.

    Questions:

    1. i have started the thread by making the my_flag variable FALSE. so once the thread is started will the program hang in the thread itself as per the following code
    Qt Code:
    1. while(!myflag)
    2. {
    3. }
    To copy to clipboard, switch view to plain text mode 
    or it will be in the main thread and constantly looking for the my_flag variable change using scheduling mechanism. i really don't understand this. (i observed that the code does not hang).

    2. By clicking the button i have made my_flag TRUE so in the thread it should go to the function readinputstatuschangeLedColourtoGreen execute it, after executing the my_flag is made FALSE. will the program automatically go to the thread or i need to start again. if i have to start the thread where and how to start the thread.

    Sorry for the lengthy mail but please let me know if you did not understand. Your reply would help me to gain little confidence on multithreading.

    thanks in advance,
    regards,
    satya
    Last edited by wysota; 2nd February 2012 at 09:22. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: multithreading simple question

    I think you should really read a bit about threading. It seems you lack basic understanding of what it does.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2012
    Posts
    20
    Thanks
    3

    Default Re: multithreading simple question

    Hi,

    Thank you for the link. it was very useful.

    regards,
    satya

Similar Threads

  1. A multithreading question
    By sepehr in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2009, 09:06
  2. simple MVC question!
    By landonmkelsey in forum Qt Programming
    Replies: 14
    Last Post: 19th August 2008, 12:25
  3. Replies: 2
    Last Post: 21st February 2008, 22:35
  4. A simple question
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 6th April 2007, 08:54
  5. simple question
    By mickey in forum Newbie
    Replies: 2
    Last Post: 20th June 2006, 10:39

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.