So i want to create a separared thread for my infinite while loop. How to fix the code below so it doesnt break the signal slot connection?
#include "gameloop.h"
GameLoop::GameLoop(QObject *parent) : QObject(parent)
{
timer = new QTimer;
connect(timer, SIGNAL(timeout()), this, SLOT(mySlot()));
timer->start(500);
thread = new QThread();
console = new Console;
console->moveToThread(thread);
thread->start();
run();
}
GameLoop::~GameLoop()
{
std::cout << "GameLoop() Destructor" << "\n";
}
void GameLoop::run()
{
console->loop();
}
void GameLoop::mySlot()
{
std::cout << "mySlot" << "\n";
}
Member function loop() in the class Console contains while(1).
The solution that solved my problem is as follows in the code below, this way the infinite loop is in a thread decicated to it:
#include "gameloop.h"
#include <QThread>
GameLoop::GameLoop(QObject *parent) : QObject(parent)
{
std::cout << "GameLoop thread: " << QThread::currentThreadId() << "\n";
timer = new QTimer;
timer->start(3000);
connect(timer, SIGNAL(timeout()), this, SLOT(mySlot())); //timers
thread1 = new QThread;
console.moveToThread(thread1);
thread1->start();
connect(thread1, SIGNAL(started()), &console, SLOT(mySlotActivationLoop()));
}
Related
I want to write data from another thread in txt file.
Text files will be named currentTime() and created periodically every minute.
So, Txt file made very well every minute using QTimer, but In Txt file, content is null.
Of course this result is correct. Because i didn't connect data from another Thread.
How to connect or send data from another thread to QTimer make the txt file every minute?
For example, another thread get data every second and QTimer make txt file every minute.
Ideally, another thread get 60 data in minute and one text file made in minute.
I want to write 60 data to txt file one cycle.
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include <QTcpSocket>
#include <QThread>
#include <QDebug>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(qintptr ID, QObject *parent = 0);
void run();
signals:
void error(QTcpSocket::SocketError socketerror);
void sendValue(QString strValue);
void close(QString disconnectId);
private slots:
void readyRead();
void disconnected();
void InitThreadObjects();
void sendData();
private:
QTcpSocket *socket;
qintptr socketDescriptor;
QByteArray Data;
};
#endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include <QThread>
#include <QTimer>
#include <QTime>
#include <QFile>
#include <QString>
#include <QTextStream>
MyThread::MyThread(qintptr ID, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
}
int count = 0;
void MyThread::run()
{
// thread starts here
qDebug() << " Thread started";
socket = new QTcpSocket();
// set the ID
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
// something's wrong, we just emit a signal
emit error(socket->error());
return;
}
// connect socket and signal
// note - Qt::DirectConnection is used because it's multithreaded
// This makes the slot to be invoked immediately, when the signal is emitted.
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(this, &MyThread::started, this, &MyThread::InitThreadObjects);
// We'll have multiple clients, we want to know which is which
qDebug() << socketDescriptor << "Client connected";
QString strValue = QString::number(socketDescriptor);
emit sendValue(strValue);
// make this thread a loop,
// thread will stay alive so that signal/slot to function properly
// not dropped out in the middle when thread dies
exec();
}
void MyThread::readyRead()
{
// get the information
Data = socket->readAll();
// will write on server side window
qDebug() << socketDescriptor << " Data in: " << Data;
socket->write(Data);
}
void MyThread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
QString disconnectId = QString::number(socketDescriptor);
emit close(disconnectId);
socket->deleteLater();
exit(0);
}
void MyThread::InitThreadObjects(){
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(sendData()));
timer->start(60000);
}
void MyThread::sendData(){
QTime currentTime = QTime::currentTime();
QString time = currentTime.toString("hhmmss");
QString filename = "/home/pi/"+time+".txt";
qDebug() << filename;
QFile file(filename);
if(!file.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "Could not open file for writing";
return;
}
QTextStream out(&file);
out << Data;
file.flush();
file.close();
}
You need to create QTimer in another thread
public slots:
void InitThreadObjects();
and in constructor add initialization
connect(this, &Thread::started, this, &Thread::InitThreadObjects);
void Thread::InitThreadObjects(){
m_Timer = new QTimer(this);
connect(m_Timer, &QTimer::timeout, this, &Thread::TimeWriteData);
m_Timer->start(60 * 1000);
}
record the collected data
void Thread::TimeWriteData(){
// Write data to file
}
I am writing multi thread server but i have problem in accept connection and start read function. i don't know where i should write them..
here is my code:
"mythread.cpp"
#include "mythread.h"
#include "myserver.h"
mythread::mythread(qintptr ID, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
}
void mythread::run()
{
qDebug() << " Thread started";
}
void mythread::acceptConnection()
{
c_client = s_server.nextPendingConnection();
connect(c_client,SIGNAL(readyRead()),
this, SLOT(startRead()));
}
void mythread::startRead()
{
char buffer[1024] = {0};
c_client->read(buffer, c_client->bytesAvailable());
qDebug() << buffer;
}
void mythread::readyRead()
{
QByteArray Data = socket->readAll();
qDebug() << socketDescriptor << " Data in: " << Data;
socket->write(Data);
}
void mythread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
socket->deleteLater();
exit(0);
}
"myserver.cpp"
#include "myserver.h"
#include "mythread.h"
myserver::myserver(QObject *parent) :
QObject(parent)
{
}
void myserver::startserver()
{
int port = 1234;
if(s_server.listen(QHostAddress::Any, port))
{
qDebug() << "Could not start server";
}
else
{
qDebug() << "Listening to port " << port ;
}
}
void myserver::incomingconnection(int socketDescriptor)
{
connect(&s_server, SIGNAL(newConnection()),
this, SLOT(acceptConnection()));
s_server.listen(QHostAddress::Any, 1234);
qDebug() << socketDescriptor << " Connecting...";
mythread *thread = new mythread(socketDescriptor,this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
i would be grateful if you help me.
You are not using QThread very well. you can use SIGNAL and SLOTS ,and MoveToThread() function. google it.
when you use QThread, the code in Run() function will be run in another thread. acceptConnection will run in main thread.
also search for nextPendingConnection();
void myserver::incomingconnection(int socketDescriptor)
{
connect(&s_server, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
...
is not OK. this connect should be called once (maybe constructor). not for any incomming connection.
I have and application with a worker thread. When I try to stop the thread and close my application it crashes.
I am creating the worker and the thread and connecting the signals and slots as:
QPointer<QThread> monitorThread(new QThread());
QPointer<ItemMonitor> monitor(new ItemMonitor(items));
monitor->moveToThread(monitorThread);
//closeInitiated signal emitted when custom close button is clicked
connect(this, SIGNAL(closeInitiated()), monitor, SLOT(finishUp()));
connect(monitor, SIGNAL(finished()), monitorThread, SLOT(quit()));
connect(monitor, SIGNAL(finished()), monitor, SLOT(deleteLater()));
connect(monitor, SIGNAL(finished()), this, SLOT(closeApplication()));
connect(monitorThread, SIGNAL(started()), monitor, SLOT(beginMonitoring()));
connect(monitorThread, SIGNAL(finished()), monitorThread, SLOT(deleteLater()));
//start monitoring
monitorThread->start();
The ItemMonitor class which is running in the QThread is
#include "itemmonitor.h"
#include "todoitem.h"
#include <QObject>
#include <iostream>
#include <QTimer>
ItemMonitor::ItemMonitor(std::vector< QPointer<ToDoItem> >& items_)
:items(items_),
shouldRun(true){
std::cout << "Monitor created" << std::endl;
}
void ItemMonitor::beginMonitoring(){
if(shouldRun){
for(int i=0; i<items.size(); i++){
items[i]->setSecsTillDeadline();
}
QTimer::singleShot(100, this, SLOT(beginMonitoring()));
}else{
emit finished();
}
}
void ItemMonitor::finishUp(){
shouldRun = false;
}
and the closeApplication function in my main class is:
void ToDoList::closeApplication(){
while(monitorThread->isRunning()){
std:: cout << "thread not closed" << std::endl;
}
QApplication::quit();
}
what is odd to me is that if I make no attempt to quit the thread and just try to close the applicationt then there are no errors
connect(monitor, SIGNAL(finished()), monitorThread, SLOT(quit()));
connect(monitorThread, SIGNAL(finished()), monitorThread, SLOT(deleteLater()));
monitor emit finished() -> monitorThread->quit() -> monitorThread emit finished() -> monitorThread->deleteLater()
after the monitors emit finished() signal, the monitorThread will be deleted.
void ToDoList::closeApplication(){
while(monitorThread->isRunning()){//maybe you should check the monitorThread for null
std:: cout << "thread not closed" << std::endl;
}
QApplication::quit();
}
While trying to make a multi-camera system work with a different thread handling a different camera, I couldn't get signals and slots working correctly between different threads. I knew something was wrong with the fact that the object sending the signal and the related slot's object were living in different threads, and thus I knew that I probably only had to find an appropriate "connection type" parameter for the connection. Eventually, I ended up discovering that only using Qt::DirectConnection would make everything work as it should.
Find the simplified code below. Here's a small description of how everything should work.
Application is the main program which is supposed to create all the threads and start them. In this simplified version, it then simply waits for the worker to finish its jobs through the slot "quit".
Worker is the object which performs one of the threaded tasks. In this simplified example, I just wait some time before finishing the computation. The worker then emits a signal which is directed to the application instance, which is then allowed to wait for all the threads and quit QCoreApplication.
What I am finding is that if I don't use Qt::DirectConnection in the second connect, then the finished() signal of the worker does not trigger the quit() slot of the thread, which means that the application then remains hanging waiting for the thread.
My question is: why is that so? since the two objects (the worker and the thread) belong to different threads, shouldn't I be using QueuedConnection, or something else? I thought DirectConnection should only be used for objects belonging to the same thread.
main.cpp:
#include <iostream>
#include <QCoreApplication>
#include "program.h"
using namespace std;
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
Program *p = new Program;
p->execute();
app.exec();
delete p;
}
program.h
#ifndef _PROGRAM_H_
#define _PROGRAM_H_
#include <QThread>
#include <QTimer>
#include "worker.h"
class Program: public QObject {
Q_OBJECT
private:
Worker *worker;
QThread *thread;
public:
void execute();
public slots:
void quit();
};
#endif // _PROGRAM_H_
program.cpp
#include "worker.h"
using namespace std;
void Program::execute() {
worker = new Worker();
thread = new QThread;
worker->moveToThread(thread);
cout << "Connection established: "
<< connect(thread, SIGNAL(started()), worker, SLOT(process()))
<< endl;
// slot not called if I remove the fifth parameter
// or if I put Qt::QueuedConnection
cout << "Connection established: "
<< connect(worker, SIGNAL(finished()), thread, SLOT(quit()),
Qt::DirectConnection)
<< endl;
cout << "Connection established: "
<< connect(worker, SIGNAL(finished()), this, SLOT(quit()))
<< endl;
cout << "Connection established: "
<< connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()))
<< endl;
cout << "Connection established: "
<< connect(worker, SIGNAL(finished()), thread, SLOT(deleteLater()))
<< endl;
thread->start();
}
void Program::quit() {
cout << "waiting.." << endl;
thread->wait();
cout << " .. I'm done!" << endl;
cout << "quitting from all.." << endl;
QCoreApplication::quit();
cout << " .. I'm done!" << endl;
}
#include "program_moc.cpp"
worker.h
#ifndef _WORKER_H_
#define _WORKER_H_
#include <QObject>
class Worker: public QObject {
Q_OBJECT
public slots:
void process();
signals:
void finished();
};
#endif // _WORKER_H_
worker.cpp:
#include <iostream>
#include <unistd.h>
#include "worker.h"
using namespace std;
void Worker::process() {
cout << "Worker::process() started" << endl;
usleep(1000000);
cout << "Worker::finished() being emitted" << endl;
emit finished();
cout << "Worker::process() finished" << endl;
}
#include "worker_moc.cpp"
EDIT
Following #ariwez 's answer does solve the problem in this specific simplified example, but it doesn't in a slightly more complex one, which I am adding now.
In this example,
Program has its own job to execute periodically through the use of a QTimer. Program also has yet another QTimer which is used to simulate the user quitting the program, which triggers the execution of the slot Program::quit().
Worker executes its own job until his quitting flag gets set to false. This is done inside Program::quit().
As in the previous example, worker successfully finishes its procedure and emits the finished() signal, which is also supposed to be connected with the thread's quit() slot. However, somehow the slot must not be executed, because Program hangs waiting for the thread. Differently from the previous example, relocating the moveToThread procedure does not solve the issue: everything works if and only if I use the Qt::DirectConnection type for the connection between Worker::finished() and QThread::quit(), and I can't understand why.
main.cpp: same as above
program.h:
#ifndef _PROGRAM_H_
#define _PROGRAM_H_
#include <QThread>
#include <QTimer>
#include "worker.h"
class Program: public QObject {
Q_OBJECT
private:
QTimer *timer, *quittingTimer;
Worker *worker;
QThread *thread;
public:
~Program();
void execute();
private slots:
void quit();
void update();
};
#endif // _PROGRAM_H_
program.cpp:
#include <iostream>
#include <QCoreApplication>
#include "program.h"
#include "worker.h"
using namespace std;
Program::~Program() {
delete timer;
delete quittingTimer;
}
void Program::execute() {
timer = new QTimer();
timer->setInterval(500);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
worker = new Worker;
thread = new QThread;
cout << "Connection established: "
<< connect(thread, SIGNAL(started()), worker, SLOT(process()))
<< endl;
// doesn't work if I remove Qt::DirectConnection
cout << "Connection established: "
<< connect(worker, SIGNAL(finished()), thread, SLOT(quit()),
Qt::DirectConnection)
<< endl;
cout << "Connection established: "
<< connect(worker, SIGNAL(finished()), this, SLOT(quit()))
<< endl;
cout << "Connection established: "
<< connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()))
<< endl;
cout << "Connection established: "
<< connect(worker, SIGNAL(finished()), thread, SLOT(deleteLater()))
<< endl;
worker->moveToThread(thread);
timer->start();
thread->start();
// simulates user pressing key to close program
quittingTimer = new QTimer();
quittingTimer->singleShot(4000, this, SLOT(quit()));
}
void Program::quit() {
cout << "timer->stop()" << endl;
timer->stop();
cout << "worker->quit()" << endl;
worker->quit();
cout << "thread->wait()" << endl;
thread->wait();
cout << "qcore->quit()" << endl;
QCoreApplication::quit();
}
void Program::update() {
cout << "Program::update() called" << endl;
}
#include "program_moc.cpp"
worker.h:
#ifndef _WORKER_H_
#define _WORKER_H_
#include <QObject>
class Worker: public QObject {
Q_OBJECT
private:
bool quit_flag;
public:
void quit();
public slots:
void process();
signals:
void finished();
};
#endif // _WORKER_H_
worker.cpp:
#include <iostream>
#include <unistd.h>
#include <QThread>
#include "worker.h"
using namespace std;
void Worker::quit() {
quit_flag = true;
}
void Worker::process() {
quit_flag = false;
while(!quit_flag) {
cout << "Worker::process() is processing" << endl;
usleep(300000);
}
cout << "Worker::finished() is being sent" << endl;
emit finished();
cout << "Worker::finished() is sent" << endl;
}
#include "worker_moc.cpp"
EDIT 2
Re-reading the article in #ariwez 's link I found out which was the problem in this second example. The problem was that the main event loop was being interrupted, while waiting for the thread's QThread::finished() signal, and so the Worker::finished() signal could not be dispatched into the QThread::quit() slot. So yeah, basically I deadlocked myself.
You moveToThread before connects that's why everything is in one thread.
I think I see what the issue might be.
Your directConnection, which is really bad thing to do accross theads as it acts like an interrupt - executs immediatley. But it is BECAUSE it executes immediatley that it works in your case. When the thread closes down it emits the quit() signal.... then usually it "quits" but in direcetConnection it immediatley executes the slot finished() and then quits.
If you have a QueuedConnection then it would emit the signal to the queue and then finish "quitting". However once the thread is stopped the thread queue is never servied so your finished() function is never run.
Perhaps a better way to do this would be to connect to your object finished() send your signal there and then get the object within the thread to stop the thread itsself. (i.e. don't stop the thread remotly, stop the object, which in turn stops its thread).
I've created my own TestService which runs on a separate QThread, but when the MainLoop terminates the QThread::finished signal does not get emitted. I saw a similar question, but the problem was slightly different there because the OP was overloading QThread whereas I simply move my class to the thread.
Note that I do not overload the QThread class, I only overload QObject based on this example: http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
Here is my TestService class:
#include <QObject>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <iostream>
using namespace std;
class TestService: public QObject
{
Q_OBJECT;
private:
volatile int _count;
QWaitCondition _monitor;
QMutex _mutex;
QThread* _thread;
public:
TestService(int numSeconds)
{
_count = numSeconds;
_thread = NULL;
cout << "TestService()" << endl;
}
virtual ~TestService()
{
cout << "~TestService()" << endl;
}
void Start()
{
QMutexLocker locker(&_mutex);
if(_thread == NULL)
{
_thread = new QThread;
// Move this service to a new thread
this->moveToThread(_thread);
// The main loop will be executed when the thread
// signals that it has started.
connect(_thread, SIGNAL(started()), this, SLOT(MainLoop()));
// Make sure that we notify ourselves when the thread
// is finished in order to correctly clean-up the thread.
connect(_thread, SIGNAL(finished()), this, SLOT(OnFinished()));
// The thread will quit when the sercives
// signals that it's finished.
connect(this, SIGNAL(Finished()), _thread, SLOT(quit()));
// The thread will be scheduled for deletion when the
// service signals that it's finished
connect(this, SIGNAL(Finished()), _thread, SLOT(deleteLater()));
// Start the thread
_thread->start();
}
}
void Stop()
{
_count = 0;
_monitor.wakeAll();
}
private slots:
void MainLoop()
{
cout << "MainLoop() Entered" << endl;
while(_count > 0)
{
cout << "T minus " << _count << " seconds." << endl;
QMutexLocker locker(&_mutex);
_monitor.wait(&_mutex, 1000);
_count--;
}
cout << "MainLoop() Finished" << endl;
emit Finished();
}
virtual void OnFinished()
{
cout << "OnFinished()" << endl;
}
signals:
void Finished();
};
Here is the testing code:
void ServiceTest()
{
cout << "Press q to quit." << endl;
cout << "Press s to start." << endl;
cout << "Press t to stop." << endl;
QSharedPointer<TestService> testService(new TestService(10));
char in = 'a';
while( in != 'q' )
{
switch(tolower(in))
{
case 's':
testService->Start();
break;
case 't':
testService->Stop();
break;
default:
break;
}
cin.get(in);
in = tolower(in);
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ServiceTest();
QTimer::singleShot(0, &a, SLOT(quit()));
return a.exec();
}
The output is:
Press q to quit.
Press s to start.
Press t to stop.
TestService()
s
MainLoop() Entered
T minus 10 seconds.
T minus 9 seconds.
T minus 8 seconds.
t
MainLoop() Finished
q
~TestService()
Press any key to continue . . .
Could anybody explain why is finished not being emitted how I can fix it?
Signal finished() gets emitted of cause, but you don't catch it.
Here:
connect(_thread, SIGNAL(finished()), this, SLOT(OnFinished()));
Qt::QueuedConnection is used, as _thread and this (service) are in different threads.
By the time finished() is emitted, _thread's event loop already finished executing, so signal will not be delivered to the slot.
You can explicitly use Qt::DirectConnection.
EDIT:
QTherad works like this:
QThread::start()
{
emit started();
run();
emit finished();
}
QThread::run()
{
eventloop->exec();
}
So, by the time finished is emitted, eventloop already stop execution. And as you move service to _thread, service's event loop is _thread event loop.
Note, that QObject itself has no its own event loop. Event loops are created by dialogs, threads and application.
Actually I will recommend in your simple case just use QtConcurent::run, as you do not perform actual event processing in the new thread, but just run single function.