[ misk0 @ 20.06.2011. 22:27 ] @
Za fax trebam napraviti neki programcic u C++ koristeci QT. Zasto, kako .. nije se mene pitalo. Uglavnom imam problem: SingleThread class: Code: #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 Code: #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); } mainwindow.h Code: 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 Citat: 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 |