ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, Has anybody got an idea how interrupt loop in C++ program?. I'll show you situation:
I've created program with Qt which turn on and off the keyboard lights with some frequency. It is done with a loop :
start()
{
while(1)
{
....
}
}
which is a part of C++ class. I execute start() when i cilck on button. But how to stop this loopback?? (i mean by clicking different buton)
I tested solutions with using function fork() to creating new process, but it doesn't want to work well (i usually received the zombie process), i think about threads, but i am not sure if it works.
How is possibly to break loops in C++ class?????
ps. breaking loop in C is really simple - enough to send a signal from other process, but i have to use a Qt classes, so i have to work in C++
You right but, but i want to break loop which exists in function1 from function2,
of course i may test - every time i make a looping - value of some global valuable (private for C++ class), and change this value in function2, but do you think it is a good idea?
It's been a while since I've done anything with inter-process communication, but I seem to recall that there is a way for one process to send a signal to another. If you fork the process before entering the loop, then have a test for "signal received from other process?" as the while condition, it should do what you're after.
I am afraid I cannot offer any details on how to do this, though...
Is there a particular reason that the loop has to be interrupted by an outside process? If a click starts the keyboard lights blinking, then shouldn't you just have a click of some kind fo stop it also? If that's the case, perhaps what you want is a loop that says "has user clicked mouse? If not, go ahead and blink lights. Loop."
I think you'll have to implement it in a thread. The problem you describe boils down to one thing. You want your program to do two things at once: wait for input from the user and blink lights in a continuous loop.
You could do it with forks, but I like threads much more.
Essentially, you will have a main thread that handles user input. Then, when you click a button, your main thread will call an interface task with your thread(s). That task will alter some internal variable for the thread; a flag to indicate some new behavior. Then your main thread will resume waiting for input.
Meanwhile, your thread's code would look something like this:
Code:
private:
int enable_blinking; // internal thread variable
int kill_thread; // flag to terminate thread's execution
...
// Public interface function
void thread_class::change_blink_status(int new_status)
{
enable_blinking = new_status;
}
// Function that is executed by the thread when it is
// created
void thread_class::start()
{
while( kill_thread == 0 )
{
if( enable_blinking == 1 )
{
toggle_lights();
// put in some delay here to get your frequency
}
}
}
It's been a while since I've played with threads, but i think that will work. It would also be cleaner to have a timer signal when to toggle the lights on or off. Lots of different ways of doing it.
Last edited by Dark_Helmet; 09-10-2003 at 10:24 AM.
Originally posted by marek I've created program with Qt which turn on and off the keyboard lights with some frequency. It is done with a loop :
start()
{
while(1)
{
....
}
}
which is a part of C++ class. I execute start() when i cilck on button. But how to stop this loopback?? (i mean by clicking different buton)
The problem is that the program does not react to the "stop"-button while the loop is running, so that's why you are thinking about threads, or a second process to interrupt it. Did I get this right?
The simplest solution, if I understand your question right, is not to use threads or processes, and not even a loop. You are using Qt, so why not use a timer ?
You can make a timer call with a (Qt slot-) function periodically (with a minimum of about 20 ms, which can be too slow in some cases, but not in your case I guess.)
Using a timer, the Qt framework does handle button-clicks and other events, while your light switching stuff is done in intervals.
Example:
Code:
//
// Header file (.h)
//
#include <qwidget.h>
#include <qtimer.h>
class yourMainWidget: public QWidget
{
Q_OBJECT
public:
yourMainWidget(QWidget *parent=0, const char *name=0);
~yourMainWidget();
// more stuff, like the buttons
protected:
void timerEvent(QTimerEvent *);
private:
int timerID;
};
//
// C++ file (.cpp)
//
void MMGameboard::timerEvent(QTimerEvent *e)
{
// do your light switching stuff here.
// *without* looping.
}
// ...then in the "start"- button slot, do:
timerID = startTimer(1000); // 1000 ms = 1 second. try 10 or 100.
// and in the "stop"-button slot, do:
killTimer(timerID);
It's me again. Hko, i am sure you have a right , i tested QTimer earlier and i created application which was working great, but i have (maybe) crazy idea to connect the old function from Linux with graphical interface (i chose Qt). Anyway i have one more question, what is it [defunct]
process -> zombie[defunct]???
Everybody knows what it is zombie, but Could you tell me what it is defunct???
Originally posted by marek but i have (maybe) crazy idea to connect the old function from Linux with graphical interface (i chose Qt). [/B]
Then why not start the old C program from your Qt app with system(), exec...() whatever. Only add a signalhandler that sets a global var, and have the loop watch that var.
Then from your Qt app, send it a signal. You can use all normal C-functions like kill(), and so in a C++/QT program.
#include <qapplication.h>
#include "zabawa.h"
#include <qpushbutton.h> //ten plik naglowkowy musi byc, zeby mozna bylo korzystac z PushButton'a dla funckji connect() !!
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form1 z;// = new Form1(0,0,FALSE,0);
z.show();
a.connect(z.startPushButton, SIGNAL(clicked() ), &z, SLOT(start_lampki() ));
a.connect(z.stopPushButton, SIGNAL(clicked() ), &z, SLOT(stop_lampki() ));
a.connect(&a, SIGNAL(lastWindowClosed() ), &a, SLOT(quit() )); //po zamknieciu ostatniego okna formatka (QDialog) bedzie zamkniety a wraz z nim cala aplikacja
a.connect(&a, SIGNAL(aboutToQuit() ), &z, SLOT(stop_lampki() )); //w momencie, w ktorym aplikacja bedzie zamykana przerwe sterowanie swiatelkami
return a.exec();
}
except these two files written above i have two files form1.h and form1.cpp (declaration and implementation for Qt class)
What about trouble - when i click on one button, it execute slot "start_lampki", and that's all application is blocked and i can't click another buttons!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.