[solved] using two or three threads
-
wrote on 16 Aug 2011, 19:56 last edited by
What tells each thread whether to generate a square or sine wave?
-
wrote on 16 Aug 2011, 20:02 last edited by
The function MyThread::run()? I'm not really sure how to do that..
-
wrote on 16 Aug 2011, 20:03 last edited by
[quote author="mlong" date="1313524604"]What tells each thread whether to generate a square or sine wave?
[/quote]
The function MyThread::run()? I’m not really sure how to do that.. -
wrote on 16 Aug 2011, 20:04 last edited by
If onWriteLoopSine() is called before onWriteLoopSquare(), onWriteLoopSine will be executed first. And if it runs forever (as implied by previous posts), onWriteLoopSquare will never get called.
-
wrote on 16 Aug 2011, 20:13 last edited by
You should tell the thread what kind of wave to use. You could use an enum for the wave types, or something.
This is a rough example that just uses a boolean flag to determine whether to use sine or not. I haven't tested it or anything.
in mythread.h:
@
protected:
void run();bool m_is_sine;
public:
explicit MyThread(QObject *parent = 0);
void setSine(bool useSine) { m_is_sine = useSine; }
@in mythread.cpp:
@MyThread::MyThread(QObject *parent) :
QThread(parent), m_is_sine(false)
{
}void MyThread::run()
{
if (m_is_sine)
onWriteLoopSine();
else
onWriteLoopSquare();
}
@in wave.cpp:
@
wave::wave(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wave)
{
ui->setupUi(this);
setup();
thread = new MyThread();
thread->setSine(true);
thread2 = new MyThread();
thread2->setSine(false);
}
@ -
wrote on 16 Aug 2011, 21:14 last edited by
Wow. Thanks alot again. I was just trying to use booleans, but i was doing it all wrong again.
This didn't quite work. The second thread which produces a square wave is producing square wave which run in the motion of sine waves. i'm checking my whole program again to see if there is anything wrong with it. -
wrote on 17 Aug 2011, 16:46 last edited by
a
-
wrote on 17 Aug 2011, 19:31 last edited by
[quote author="mlong" date="1313525638"]You should tell the thread what kind of wave to use. You could use an enum for the wave types, or something.
This is a rough example that just uses a boolean flag to determine whether to use sine or not. I haven't tested it or anything.
in mythread.h:
@
protected:
void run();bool m_is_sine;
public:
explicit MyThread(QObject *parent = 0);
void setSine(bool useSine) { m_is_sine = useSine; }
@in mythread.cpp:
@MyThread::MyThread(QObject *parent) :
QThread(parent), m_is_sine(false)
{
}void MyThread::run()
{
if (m_is_sine)
onWriteLoopSine();
else
onWriteLoopSquare();
}
@in wave.cpp:
@
wave::wave(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wave)
{
ui->setupUi(this);
setup();
thread = new MyThread();
thread->setSine(true);
thread2 = new MyThread();
thread2->setSine(false);
}
@[/quote]What if I had three threads, so I would need three conditions instead of 2?
-
wrote on 17 Aug 2011, 19:38 last edited by
The big picture is this...
You need some way to tell the thread what kind of wave to generate. Think of it as a property of the thread class.
I already suggested you could use an "enum":http://cplus.about.com/od/introductiontoprogramming/p/enumeration.htm to do it.
But figuring out how you want to do it is up to you.
-
wrote on 17 Aug 2011, 19:43 last edited by
Thanks mlong. I just included a second boolean.:)
Here's the solution:
mythread.cpp:
@#include "wave.h"
#include "mythread.h"MyThread::MyThread(QObject *parent) :
QThread(parent), is_sine(true), is_square(true)
{
}void MyThread::run()
{
if (is_sine) onWriteLoopSine();
else {
if(is_square) onWriteLoopSquare();
else onWriteLoopTri();
}
}@mythread.h:
@#ifndef MYTHREAD_H
#define MYTHREAD_H#include <QThread>
class MyThread : public QThread
{
Q_OBJECTprotected:
void run();
bool is_sine;
bool is_square;public:
explicit MyThread(QObject *parent = 0);
void setSine(bool useSine) { is_sine = useSine;}
void setSquare(bool useSquare) { is_square = useSquare;}};
#endif // MYTHREAD_H@
wave.cpp:
@wave::wave(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wave)
{
ui->setupUi(this);
setup();
sinethread = new MyThread();
sinethread->setSine(true);
squarethread = new MyThread();
squarethread->setSine(false);
squarethread->setSquare(true);
trithread = new MyThread();
trithread->setSine(false);
trithread->setSquare(false);}@
11/11