|
21. 06. 2011. | #1 | |
majstor
Wrote a book
|
QT newbie.. problem sa klasom
Za fax trebam napraviti neki programcic u C++ koristeci QT. Zasto, kako .. nije se mene pitalo.
Uglavnom imam problem: SingleThread class: Kôd:
#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 Kôd:
#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, ¶ms ); 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, ¶ms ); qDebug("Set success: %d | sched_priority: %d", s, params.sched_priority); qDebug("Pointer Address: %p \n\n", ¶ms); }; 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, ¶ms ); qDebug("Set success: %d \n", s); pthread_getschedparam(pthread_self(), &policy, ¶ms ); qDebug("Policy: %d == Priority: %d\n", policy, params.sched_priority); } Kôd:
private: SingleThread thread_1, thread_2, thread_3, thread_4, thread_5; Po startovanju app dobijam ovaj output Citat:
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
__________________
|
|
02. 07. 2011. | #2 |
majstor
Wrote a book
|
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.
__________________
|
|
|