Results 1 to 15 of 15

Thread: A while(1) C function to run in Qt

  1. #1
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default A while(1) C function to run in Qt

    Hello Guy,
    I trying to put a while(1) C function in my Qt project.
    I want to run it in the background of a specific Gui.
    I have been trying to use threads to do it .I tried declaring the thread in main() and my Gui cpp file(infuison.cpp) But there seem to be a problem.
    this is what i have done.
    QThread: Destroyed while thread is still running
    ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file thread\qthread_win.cpp, line 574
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.

    Qt Code:
    1. //threading.h
    2. #ifndef THREADINGS_H
    3. #define THREADINGS_H
    4.  
    5. #include <QObject>
    6. #include <QThread>
    7.  
    8. class Threadings : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit Threadings(QObject *parent = 0);
    13. void DoSetup(QThread &cThread);
    14. signals:
    15.  
    16. public slots:
    17. void runAlgoMain();
    18.  
    19. };
    20.  
    21. #endif // THREADINGS_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //threadings.cpp
    2.  
    3. #include "threadings.h"
    4.  
    5. extern "C++"
    6. {
    7. #include "test.h"
    8. }
    9.  
    10. Threadings::Threadings(QObject *parent) :
    11. QObject(parent)
    12. {
    13. }
    14.  
    15. void Threadings::DoSetup(QThread &cThread)
    16. {
    17. connect(&cThread,SIGNAL(started()),this,SLOT(runAlgoMain()));
    18. }
    19.  
    20. void Threadings::runAlgoMain()
    21. {
    22. runAlgoMain();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include infusion.cpp
    2. #include "threadings.h"
    3. Infusion::Infusion(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Infusion)
    6. {
    7. QThread cThread;
    8. Threadings cThreadings;
    9. cThreadings.DoSetup(cThread);
    10. cThreadings.moveToThread(&cThread);
    11. cThread.start();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Am i doing threading right ? Please help

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: A while(1) C function to run in Qt

    Am i doing threading right ?
    Clearly not.
    What is the lifetime of the QThread object at line 7 of your last listing?

    See How To Really, Truly Use QThreads; The Full Explanation

  3. #3
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    So i have to make an instance of a new qthread and put my function into it?
    But how am i going to move my Function into the thread ? eg Worker *worker=new Worker . How to pass when my Function is an .c not a c++?
    Anyway can we use QTimer to make my c functions run in the background just like threads ?
    I am unsure of the Puclic slot :finished ();, from the link you gave me .

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

    Default Re: A while(1) C function to run in Qt

    C++ compilers don't care if you call "C" or "C++" functions in your programs.
    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
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by wysota View Post
    C++ compilers don't care if you call "C" or "C++" functions in your programs.
    Oh wait, My "C" functions are in a .C file . So i am unable to do Worker *worker=new Worker at all .

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: A while(1) C function to run in Qt

    Wrap your plain functions in a class and the problem is solved. It really is not complicated. Here is a complete, threaded program that prints powers of two:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QObject>
    3. #include <QThread>
    4. #include <QDebug>
    5.  
    6. // Plain function with C linkage declaration
    7. extern "C" {
    8. int multiplyByTwo(int i);
    9. }
    10.  
    11. // Plain function impl
    12. int multiplyByTwo(int i) {
    13. return i * 2;
    14. }
    15.  
    16.  
    17.  
    18. class Worker : public QObject {
    19. Q_OBJECT
    20.  
    21. public:
    22. Worker();
    23. ~Worker();
    24. public slots:
    25. void process();
    26. signals:
    27. void finished();
    28. private:
    29. // add your variables here
    30. };
    31.  
    32. Worker::Worker() { }
    33. Worker::~Worker() { }
    34.  
    35. // Start processing data.
    36. void Worker::process() {
    37. int value = 1;
    38. while (value < 1000000000) {
    39. qDebug() << value;
    40. value = multiplyByTwo(value);
    41. }
    42. emit finished();
    43. }
    44.  
    45.  
    46. int main(int argc, char **argv) {
    47. QCoreApplication app(argc, argv);
    48.  
    49. QThread* thread = new QThread;
    50. Worker* worker = new Worker;
    51. worker->moveToThread(thread);
    52. QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
    53. QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    54. QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    55. QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    56. thread->start();
    57.  
    58. QObject::connect(thread, SIGNAL(destroyed()), qApp, SLOT(quit()));
    59. return app.exec();
    60. }
    61. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    Thanks guys i have found another way to "Threading ", maybe wrongly executed .
    I did it by declaring the "C" function into a function in the cpp file .
    Then i used a Qtimer to connect the timer to the function and start it.

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

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by 020394 View Post
    Thanks guys i have found another way to "Threading ", maybe wrongly executed .
    I did it by declaring the "C" function into a function in the cpp file .
    Then i used a Qtimer to connect the timer to the function and start it.
    There is no threading here, timers do not spawn new threads.
    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.


  9. #9
    Join Date
    Jul 2013
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: A while(1) C function to run in Qt

    Try to use the QtConcurrent::run or the QtConcurrent::map instead the QThread.

  10. #10
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    So when can I use the QTimer ? I am trying to use the QTimer to run a function along with the Gui. Is it the right way to ?

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

    Default Re: A while(1) C function to run in Qt

    A timer is for running some code in a specific moment in time. It is handled by an event like (almost) everything else in Qt.
    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.


  12. #12
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by wysota View Post
    A timer is for running some code in a specific moment in time. It is handled by an event like (almost) everything else in Qt.
    Isnt It the same as running threads ? For now i am doing a school project with different groups all doing different part . for me i am doing the Gui. I need to integrate the rest of the codes my friends do . EG (Algorithm , Checking of a keypad. etc). Am i able to use QTimer for them since i want them to run at a specific moment of time when i am at the Gui page

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

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by 020394 View Post
    Isnt It the same as running threads ?
    No. If you are to eat lunch at 12 o'clock, it doesn't mean at 12 some other instance of yourself is spawned and eats a sandwich but rather you have to stop what you had been doing, take out a sandwich, unwrap it, eat it and then attend your other tasks.
    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.


  14. The following user says thank you to wysota for this useful post:

    anda_skoa (22nd July 2013)

  15. #14
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A while(1) C function to run in Qt

    I see so threading is always running in the background . But i been using timer to check the current time(Updatiing the time on the Label) , Isnt that considered as threadings?

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

    Default Re: A while(1) C function to run in Qt

    Quote Originally Posted by 020394 View Post
    I see so threading is always running in the background . But i been using timer to check the current time(Updatiing the time on the Label) , Isnt that considered as threadings?
    No. Timers have nothing to do with threading.
    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.


Similar Threads

  1. pass member function as argument int template function
    By bibhukalyana in forum General Programming
    Replies: 1
    Last Post: 12th March 2013, 07:05
  2. Replies: 11
    Last Post: 5th September 2012, 20:47
  3. Replies: 4
    Last Post: 2nd August 2012, 07:42
  4. Replies: 3
    Last Post: 25th May 2010, 09:46
  5. Replies: 0
    Last Post: 10th March 2010, 08:13

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.