PDA

Pogčedajte punu verziju : QT newbie.. problem sa klasom


misk0
20. 06. 2011., 23:49
Za fax trebam napraviti neki programcic u C++ koristeci QT. Zasto, kako .. nije se mene pitalo.
Uglavnom imam problem:


SingleThread class:


#include <QThread>
#include "pthread.h"

#ifndef SINGLETHREAD_H
#define SINGLETHREAD_H

class SingleThread : public QThread
{
Q_OBJECT;

public:
SingleThread();
void stopThread();
void changePriority(int);

protected:
void run();

signals:
void updateProgress(int);

private:
int counter, direction;
bool working;
struct sched_param params;

};
#endif // SINGLETHREAD_H



#include "SingleThread.h"
#include <stdio.h>
#include <iostream>

SingleThread::SingleThread()
{
counter = 0;
working = true;
direction = 1;
int policy, s;

s = pthread_getschedparam(pthread_self(), &policy, &params );
qDebug("Read success = %d | policy: %d | sched_priority: %d", s, policy, params.sched_priority);
params.sched_priority = 1;

s = pthread_setschedparam(pthread_self(), SCHED_RR, &params );

qDebug("Set success: %d | sched_priority: %d", s, params.sched_priority);
qDebug("Pointer Address: %p \n\n", &params);
};

void SingleThread::run()
{

working = true;

std::cout << "Thread::run()" << std::endl;
while (working){
counter += direction;
emit updateProgress(counter);
msleep(30);
if ( counter > 99)
{
direction = -1;
} else if ( counter < 1)
{
direction = 1;
}
}

};

void SingleThread::stopThread() {
working = false;
}

void SingleThread::changePriority(int newPriority) {
int policy,s ;
params.sched_priority = newPriority;

s = pthread_setschedparam(pthread_self(), SCHED_RR, &params );
qDebug("Set success: %d \n", s);

pthread_getschedparam(pthread_self(), &policy, &params );
qDebug("Policy: %d == Priority: %d\n", policy, params.sched_priority);

}


mainwindow.h

private:
SingleThread thread_1, thread_2, thread_3, thread_4, thread_5;


tu deklarishem ove thread-ove i koliko kontam automatski po inicijalizaciji mainwindow-a threadovi su kreirani i pozvan je konstruktor metod thread.

Po startovanju app dobijam ovaj output

root@ubuntu-vm:/home/misk0/qt/Test1# sudo ./Test1
Read success = 0 | policy: 0 | sched_priority: 0
Set success: 0 | sched_priority: 1
Pointer Address: 0xbfd49794


Read success = 0 | policy: 2 | sched_priority: 1
Set success: 0 | sched_priority: 1
Pointer Address: 0xbfd497ac


Read success = 0 | policy: 2 | sched_priority: 1
Set success: 0 | sched_priority: 1
Pointer Address: 0xbfd497c4


Read success = 0 | policy: 2 | sched_priority: 1
Set success: 0 | sched_priority: 1
Pointer Address: 0xbfd497dc


Read success = 0 | policy: 2 | sched_priority: 1
Set success: 0 | sched_priority: 1
Pointer Address: 0xbfd497f4



Primjetite da prvi kreiran thread ima policy: 0 i sched_priority 0 a svi ostali imaju druge vrijednosti.
Ne kontam zasto i kako? Imam osjecaj da je neka varijabla djeljena izmedju instanci thread-ova ali ne mogu skontati koja..

hvalaa

misk0
02. 07. 2011., 19:31
Vec rijesio ali mozda zatreba jos nekome. Kad se QThread instancira, u tom momentu thread jos ne postoji i pthread_self() vraca thread kreator (glavni thread app-a u mom slucaju).

Tek kad se pozove thread.start() tj metod run(), u tom momentu je kreiran novi thread.