Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, August 2007
Starting QProcess from QThread: misunderstanding...


Message 1 in thread

Hi,

System:
    Linux (Ubuntu 7.04)

Problem:
    I have a QMainWindow GUI and need to start some shell script. I use 
separate QThread as a QProcess manager. Thus the logic is next one:
        1. Create Class that inherits from QThread. (let's call it ClassA).
        2. Inside ClassA:
              a) Constructor: create QProcess [ Let's say this way: 
QProcess *poProcess = new QProcess(); ]
              b) Override QThread::run() and start process from there:
                    const int nRESULT = poProcess->execute( 
"./hello_world.sh", QStringList() << "name1" << "name2");   (*)

This works fine. QThread freezes at (*) and waits for Process to finish.
Right after (*) I try to read all cout of process:
    if( !nRESULT) {
       QString oCout = poProcess->readAllStandardOutput();

       std::cout << oCout.toStdString() << std::endl;   // Nothing 
actually printed
    }
But I am sure that scripts prints out some line with echo like:
    echo "Hello world $1 $2"

Question: What am I doing wrong here?

Thank you, Samvel.

--
 [ signature omitted ] 

Message 2 in thread

On Friday 10 August 2007, Samvel Khalatian wrote:
> Hi,
>
> System:
>     Linux (Ubuntu 7.04)
>
> Problem:
>     I have a QMainWindow GUI and need to start some shell script. I use
> separate QThread as a QProcess manager. Thus the logic is next one:
>         1. Create Class that inherits from QThread. (let's call it ClassA).
>         2. Inside ClassA:
>               a) Constructor: create QProcess [ Let's say this way:
> QProcess *poProcess = new QProcess(); ]
>               b) Override QThread::run() and start process from there:
>                     const int nRESULT = poProcess->execute(
> "./hello_world.sh", QStringList() << "name1" << "name2");   (*)
>
> This works fine. QThread freezes at (*) and waits for Process to finish.
> Right after (*) I try to read all cout of process:
>     if( !nRESULT) {
>        QString oCout = poProcess->readAllStandardOutput();
>
>        std::cout << oCout.toStdString() << std::endl;   // Nothing
> actually printed
>     }
> But I am sure that scripts prints out some line with echo like:
>     echo "Hello world $1 $2"
>
> Question: What am I doing wrong here?

QProcess::execute() is a static function and will not change the state of your 
poProcess instance. If you need to get the output from the process you need 
to use QProcess::start() . For more info see the docs. 

>
> Thank you, Samvel.



-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 3 in thread

Hi,

Perfect. Your answer cleared up many things.
Now new question arose.

I forgot to write that Qt 4.2 is used for programming.

Problem: Start QProcess from QThread with start method.
I wrote a simple test program that starts QProcess with 
QProcess::start() method from QMainWindow (main Thread). Besides 
QProcess signals are connected to QMainWindow user slots and everything 
works perfectly. I can manage QProcess from QMainWindow including 
starting it up, killling, terminating, etc. Signals<->Slots connections 
work fine.

Now I try to do the same but with QThread. Scenarion is next:
    1. Create QThread from QMainWindow that has QProcess (inside QThread).
    2. Start QProcess in QThread::run() overridden method. A

Here is my code:

--[ ProcessThread.h ]--
// Author : Samvel Khalatian (ksamdev at gmail dot com)
// Created: 08/10/07
// License: GPL

#ifndef PROCESS_THREAD_H
#define PROCESS_THREAD_H

#include <QThread>
#include <QProcess>

class QMutex;

class ProcessThread: public QThread
{
  Q_OBJECT

  private slots:
    void onProcessFinished( int nExitCode, QProcess::ExitStatus 
eExitStatus);

  public:
    ProcessThread( QObject *poParent = 0);
    virtual ~ProcessThread();

    void stop();

  private:
    void run();

    QProcess *poProcess_;
    QMutex   *poMutex_;
    bool      bStopped_;
    bool      bProcessRunning_;
};

#endif // PROCESS_THREAD_H

--[ ProcessThread.cc ]--
// Author : Samvel Khalatian (ksamdev at gmail dot com)
// Created: 08/10/07
// License: GPL

#include <iostream>

#include <QtCore>
#include <QMutex>
#include <QMutexLocker>

#include "interface/ProcessThread.h"

// --[ PROCESS THREAD 
]--------------------------------------------------------
//                                                  --[ PUBLIC SLOTS ]--

//                                                  --[ PUBLIC ]--
ProcessThread::ProcessThread( QObject *poParent)
  : QThread( poParent),
    poProcess_( new QProcess( this)),
    poMutex_( new QMutex()),
    bStopped_( false),
    bProcessRunning_( false)
{
  connect( poProcess_, SIGNAL( finished         ( int, 
QProcess::ExitStatus)),
           this      , SLOT  ( onProcessFinished( int, 
QProcess::ExitStatus)) );
}

ProcessThread::~ProcessThread() {
  delete poMutex_;
  delete poProcess_;
}

void ProcessThread::stop() {
  QMutexLocker oLocker( poMutex_);

  if( bProcessRunning_) {
    bProcessRunning_ = false;
    poProcess_->terminate();
  }

  bStopped_ = true;
}

//                                                  --[ PRIVATE SLOTS ]--
void ProcessThread::onProcessFinished( int nExitCode,
                                       QProcess::ExitStatus eExitStatus)
{
  std::cout << "[\033[1mthread\033[0m] process finished" << std::endl;
  QMutexLocker oLocker( poMutex_);

  bStopped_ = true;
  bProcessRunning_ = false;
}

//                                                  --[ PRIVATE ]--     
void ProcessThread::run() {
  {
    QMutexLocker oLocker( poMutex_);
    poProcess_->start( "sleep 5");
    poProcess_->waitForStarted();
    bProcessRunning_ = true;
  }

  forever {
    QMutexLocker oLocker( poMutex_);
    if( bStopped_) {
      bStopped_ = false;
      break;
    }
  }
}

You may easily try given class.
The issue is that ProcessThread::onProcessFinished(...) is never called 
in normal way only if I close application. Why? It seems like 
Signals<->Slots connections work in a strange way (I am missing some key 
point here... what's wrong?) :( and all problems described before arise 
again. Besides that I get error:

QObject: Cannot create children for a parent that is in a different thread.
QObject: Cannot create children for a parent that is in a different thread.
QObject: Cannot create children for a parent that is in a different thread.
QObject: Cannot create children for a parent that is in a different thread.
QObject: Cannot create children for a parent that is in a different thread.

whenever QProcess::start() is called /aka in QThread::run()/.

Is there any restriction on use of QProcess'es from QThread? Am I doing 
something wrong?

Thank you, Samvel.


Iulian M wrote:
> On Friday 10 August 2007, Samvel Khalatian wrote:
>   
>> Hi,
>>
>> System:
>>     Linux (Ubuntu 7.04)
>>
>> Problem:
>>     I have a QMainWindow GUI and need to start some shell script. I use
>> separate QThread as a QProcess manager. Thus the logic is next one:
>>         1. Create Class that inherits from QThread. (let's call it ClassA).
>>         2. Inside ClassA:
>>               a) Constructor: create QProcess [ Let's say this way:
>> QProcess *poProcess = new QProcess(); ]
>>               b) Override QThread::run() and start process from there:
>>                     const int nRESULT = poProcess->execute(
>> "./hello_world.sh", QStringList() << "name1" << "name2");   (*)
>>
>> This works fine. QThread freezes at (*) and waits for Process to finish.
>> Right after (*) I try to read all cout of process:
>>     if( !nRESULT) {
>>        QString oCout = poProcess->readAllStandardOutput();
>>
>>        std::cout << oCout.toStdString() << std::endl;   // Nothing
>> actually printed
>>     }
>> But I am sure that scripts prints out some line with echo like:
>>     echo "Hello world $1 $2"
>>
>> Question: What am I doing wrong here?
>>     
>
> QProcess::execute() is a static function and will not change the state of your 
> poProcess instance. If you need to get the output from the process you need 
> to use QProcess::start() . For more info see the docs. 
>
>   
>> Thank you, Samvel.
>>     
>
>
>
>   


Message 4 in thread

On Friday 10 August 2007, Samvel Khalatian wrote:
[snip]
>
> You may easily try given class.
> The issue is that ProcessThread::onProcessFinished(...) is never called
> in normal way only if I close application. Why? It seems like
> Signals<->Slots connections work in a strange way (I am missing some key
> point here... what's wrong?) :( and all problems described before arise
> again. Besides that I get error:
>
> QObject: Cannot create children for a parent that is in a different thread.
> QObject: Cannot create children for a parent that is in a different thread.
> QObject: Cannot create children for a parent that is in a different thread.
> QObject: Cannot create children for a parent that is in a different thread.
> QObject: Cannot create children for a parent that is in a different thread.
>
Hi,

Read http://doc.trolltech.com/4.2/threads.html very carefully. 

In the "Threads and QObjects" section you will find the answer:
"The child of a QObject must always be created in the thread where the parent 
was created. This implies, among other things, that you should never pass the 
QThread object (this) as the parent of an object created in the thread (since 
the QThread object itself was created in another thread)."

Hint: Review your ProcessThread constructor.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 5 in thread

I think you need to add exec() to do event handling in a thread.
Probably somewhere in the run() function.
 
 

	-----Original Message-----
	From: Samvel Khalatian [mailto:ksamdev@xxxxxxxxx] 
	Sent: Friday, August 10, 2007 09:47
	To: Iulian M; qt-interest@xxxxxxxxxxxxx
	Subject: Re: Starting QProcess from QThread: misunderstanding...
	
	
	Hi, 
	
	Perfect. Your answer cleared up many things. 
	Now new question arose.
	
	I forgot to write that Qt 4.2 is used for programming.
	
	Problem: Start QProcess from QThread with start method.
	I wrote a simple test program that starts QProcess with
QProcess::start() method from QMainWindow (main Thread). Besides
QProcess signals are connected to QMainWindow user slots and everything
works perfectly. I can manage QProcess from QMainWindow including
starting it up, killling, terminating, etc. Signals<->Slots connections
work fine.
	
	Now I try to do the same but with QThread. Scenarion is next:
	    1. Create QThread from QMainWindow that has QProcess (inside
QThread).
	    2. Start QProcess in QThread::run() overridden method. A
	
	Here is my code:
	
	--[ ProcessThread.h ]--
	// Author : Samvel Khalatian (ksamdev at gmail dot com)
	// Created: 08/10/07
	// License: GPL
	
	#ifndef PROCESS_THREAD_H
	#define PROCESS_THREAD_H
	
	#include <QThread>
	#include <QProcess>
	
	class QMutex;
	
	class ProcessThread: public QThread
	{
	  Q_OBJECT
	
	  private slots:
	    void onProcessFinished( int nExitCode, QProcess::ExitStatus
eExitStatus);
	
	  public:
	    ProcessThread( QObject *poParent = 0);
	    virtual ~ProcessThread();
	
	    void stop();
	
	  private:
	    void run();
	
	    QProcess *poProcess_;
	    QMutex   *poMutex_;
	    bool      bStopped_;
	    bool      bProcessRunning_;
	};
	
	#endif // PROCESS_THREAD_H
	
	--[ ProcessThread.cc ]--
	// Author : Samvel Khalatian (ksamdev at gmail dot com)
	// Created: 08/10/07
	// License: GPL
	
	#include <iostream>
	
	#include <QtCore>
	#include <QMutex>
	#include <QMutexLocker>
	
	#include "interface/ProcessThread.h"
	
	// --[ PROCESS THREAD
]--------------------------------------------------------
	//                                                  --[ PUBLIC
SLOTS ]--
	
	//                                                  --[ PUBLIC
]--
	ProcessThread::ProcessThread( QObject *poParent)
	  : QThread( poParent),
	    poProcess_( new QProcess( this)),
	    poMutex_( new QMutex()),
	    bStopped_( false),
	    bProcessRunning_( false)
	{
	  connect( poProcess_, SIGNAL( finished         ( int,
QProcess::ExitStatus)),
	           this      , SLOT  ( onProcessFinished( int,
QProcess::ExitStatus)) );
	}
	
	ProcessThread::~ProcessThread() {
	  delete poMutex_;
	  delete poProcess_;
	}
	
	void ProcessThread::stop() {
	  QMutexLocker oLocker( poMutex_);
	
	  if( bProcessRunning_) {
	    bProcessRunning_ = false;
	    poProcess_->terminate();
	  }
	
	  bStopped_ = true;
	}
	
	//                                                  --[ PRIVATE
SLOTS ]--
	void ProcessThread::onProcessFinished( int nExitCode,
	                                       QProcess::ExitStatus
eExitStatus)
	{
	  std::cout << "[\033[1mthread\033[0m] process finished" <<
std::endl;
	  QMutexLocker oLocker( poMutex_);
	
	  bStopped_ = true;
	  bProcessRunning_ = false;
	}
	
	//                                                  --[ PRIVATE
]--      
	void ProcessThread::run() {
	  {
	    QMutexLocker oLocker( poMutex_);
	    poProcess_->start( "sleep 5");
	    poProcess_->waitForStarted();
	    bProcessRunning_ = true;
	  }
	
	  forever {
	    QMutexLocker oLocker( poMutex_);
	    if( bStopped_) {
	      bStopped_ = false;
	      break;
	    }
	  }
	}
	
	You may easily try given class.
	The issue is that ProcessThread::onProcessFinished(...) is never
called in normal way only if I close application. Why? It seems like
Signals<->Slots connections work in a strange way (I am missing some key
point here... what's wrong?) :( and all problems described before arise
again. Besides that I get error:
	
	QObject: Cannot create children for a parent that is in a
different thread.
	QObject: Cannot create children for a parent that is in a
different thread.
	QObject: Cannot create children for a parent that is in a
different thread.
	QObject: Cannot create children for a parent that is in a
different thread.
	QObject: Cannot create children for a parent that is in a
different thread.
	
	whenever QProcess::start() is called /aka in QThread::run()/.
	
	Is there any restriction on use of QProcess'es from QThread? Am
I doing something wrong?
	
	Thank you, Samvel.
	
	
	Iulian M wrote: 

		On Friday 10 August 2007, Samvel Khalatian wrote:
		  

			Hi,
			
			System:
			    Linux (Ubuntu 7.04)
			
			Problem:
			    I have a QMainWindow GUI and need to start
some shell script. I use
			separate QThread as a QProcess manager. Thus the
logic is next one:
			        1. Create Class that inherits from
QThread. (let's call it ClassA).
			        2. Inside ClassA:
			              a) Constructor: create QProcess [
Let's say this way:
			QProcess *poProcess = new QProcess(); ]
			              b) Override QThread::run() and
start process from there:
			                    const int nRESULT =
poProcess->execute(
			"./hello_world.sh", QStringList() << "name1" <<
"name2");   (*)
			
			This works fine. QThread freezes at (*) and
waits for Process to finish.
			Right after (*) I try to read all cout of
process:
			    if( !nRESULT) {
			       QString oCout =
poProcess->readAllStandardOutput();
			
			       std::cout << oCout.toStdString() <<
std::endl;   // Nothing
			actually printed
			    }
			But I am sure that scripts prints out some line
with echo like:
			    echo "Hello world $1 $2"
			
			Question: What am I doing wrong here?
			    

		
		QProcess::execute() is a static function and will not
change the state of your 
		poProcess instance. If you need to get the output from
the process you need 
		to use QProcess::start() . For more info see the docs. 
		
		  

			Thank you, Samvel.
			    

		
		
		
		  



Message 6 in thread

Hmm, I don't think exec() should solve given problem. Exec() is a 
functions (the same idea as QApplication::exec() ):
     You enter Event Loop and Exit it on exec() return. Thus if I write 
something like:

void MyThread::run() {
...
exec(); // Events processing within this function
...
}

BTW, everything works fine now, thanks to Iulian M notes. I have QHttp 
working with WEB resources. The only annoying problem left is that I 
always get a message like:
QObject: can not kill timers

The idea is to have run() method and create QHttp instance in the 
beginning and kill it at the end. Of course in between there is an 
infinite loop that can be exited only once all QHttp requests are done.

Any ideas of why could it happen: why do I get that error message? It 
seems to be odd.

Samvel.

Jones, Torrin A (US SSA) wrote:
> I think you need to add exec() to do event handling in a thread.  
> Probably somewhere in the run() function.
>  
>  
>
>     -----Original Message-----
>     *From:* Samvel Khalatian [mailto:ksamdev@xxxxxxxxx]
>     *Sent:* Friday, August 10, 2007 09:47
>     *To:* Iulian M; qt-interest@xxxxxxxxxxxxx
>     *Subject:* Re: Starting QProcess from QThread: misunderstanding...
>
>     Hi,
>
>     Perfect. Your answer cleared up many things.
>     Now new question arose.
>
>     I forgot to write that Qt 4.2 is used for programming.
>
>     Problem: Start QProcess from QThread with start method.
>     I wrote a simple test program that starts QProcess with
>     QProcess::start() method from QMainWindow (main Thread). Besides
>     QProcess signals are connected to QMainWindow user slots and
>     everything works perfectly. I can manage QProcess from QMainWindow
>     including starting it up, killling, terminating, etc.
>     Signals<->Slots connections work fine.
>
>     Now I try to do the same but with QThread. Scenarion is next:
>         1. Create QThread from QMainWindow that has QProcess (inside
>     QThread).
>         2. Start QProcess in QThread::run() overridden method. A
>
>     Here is my code:
>
>     --[ ProcessThread.h ]--
>     // Author : Samvel Khalatian (ksamdev at gmail dot com)
>     // Created: 08/10/07
>     // License: GPL
>
>     #ifndef PROCESS_THREAD_H
>     #define PROCESS_THREAD_H
>
>     #include <QThread>
>     #include <QProcess>
>
>     class QMutex;
>
>     class ProcessThread: public QThread
>     {
>       Q_OBJECT
>
>       private slots:
>         void onProcessFinished( int nExitCode, QProcess::ExitStatus
>     eExitStatus);
>
>       public:
>         ProcessThread( QObject *poParent = 0);
>         virtual ~ProcessThread();
>
>         void stop();
>
>       private:
>         void run();
>
>         QProcess *poProcess_;
>         QMutex   *poMutex_;
>         bool      bStopped_;
>         bool      bProcessRunning_;
>     };
>
>     #endif // PROCESS_THREAD_H
>
>     --[ ProcessThread.cc ]--
>     // Author : Samvel Khalatian (ksamdev at gmail dot com)
>     // Created: 08/10/07
>     // License: GPL
>
>     #include <iostream>
>
>     #include <QtCore>
>     #include <QMutex>
>     #include <QMutexLocker>
>
>     #include "interface/ProcessThread.h"
>
>     // --[ PROCESS THREAD
>     ]--------------------------------------------------------
>     //                                                  --[ PUBLIC
>     SLOTS ]--
>
>     //                                                  --[ PUBLIC ]--
>     ProcessThread::ProcessThread( QObject *poParent)
>       : QThread( poParent),
>         poProcess_( new QProcess( this)),
>         poMutex_( new QMutex()),
>         bStopped_( false),
>         bProcessRunning_( false)
>     {
>       connect( poProcess_, SIGNAL( finished         ( int,
>     QProcess::ExitStatus)),
>                this      , SLOT  ( onProcessFinished( int,
>     QProcess::ExitStatus)) );
>     }
>
>     ProcessThread::~ProcessThread() {
>       delete poMutex_;
>       delete poProcess_;
>     }
>
>     void ProcessThread::stop() {
>       QMutexLocker oLocker( poMutex_);
>
>       if( bProcessRunning_) {
>         bProcessRunning_ = false;
>         poProcess_->terminate();
>       }
>
>       bStopped_ = true;
>     }
>
>     //                                                  --[ PRIVATE
>     SLOTS ]--
>     void ProcessThread::onProcessFinished( int nExitCode,
>                                            QProcess::ExitStatus
>     eExitStatus)
>     {
>       std::cout << "[\033[1mthread\033[0m] process finished" << std::endl;
>       QMutexLocker oLocker( poMutex_);
>
>       bStopped_ = true;
>       bProcessRunning_ = false;
>     }
>
>     //                                                  --[ PRIVATE
>     ]--     
>     void ProcessThread::run() {
>       {
>         QMutexLocker oLocker( poMutex_);
>         poProcess_->start( "sleep 5");
>         poProcess_->waitForStarted();
>         bProcessRunning_ = true;
>       }
>
>       forever {
>         QMutexLocker oLocker( poMutex_);
>         if( bStopped_) {
>           bStopped_ = false;
>           break;
>         }
>       }
>     }
>
>     You may easily try given class.
>     The issue is that ProcessThread::onProcessFinished(...) is never
>     called in normal way only if I close application. Why? It seems
>     like Signals<->Slots connections work in a strange way (I am
>     missing some key point here... what's wrong?) :( and all problems
>     described before arise again. Besides that I get error:
>
>     QObject: Cannot create children for a parent that is in a
>     different thread.
>     QObject: Cannot create children for a parent that is in a
>     different thread.
>     QObject: Cannot create children for a parent that is in a
>     different thread.
>     QObject: Cannot create children for a parent that is in a
>     different thread.
>     QObject: Cannot create children for a parent that is in a
>     different thread.
>
>     whenever QProcess::start() is called /aka in QThread::run()/.
>
>     Is there any restriction on use of QProcess'es from QThread? Am I
>     doing something wrong?
>
>     Thank you, Samvel.
>
>
>     Iulian M wrote:
>>     On Friday 10 August 2007, Samvel Khalatian wrote:
>>       
>>>     Hi,
>>>
>>>     System:
>>>         Linux (Ubuntu 7.04)
>>>
>>>     Problem:
>>>         I have a QMainWindow GUI and need to start some shell script. I use
>>>     separate QThread as a QProcess manager. Thus the logic is next one:
>>>             1. Create Class that inherits from QThread. (let's call it ClassA).
>>>             2. Inside ClassA:
>>>                   a) Constructor: create QProcess [ Let's say this way:
>>>     QProcess *poProcess = new QProcess(); ]
>>>                   b) Override QThread::run() and start process from there:
>>>                         const int nRESULT = poProcess->execute(
>>>     "./hello_world.sh", QStringList() << "name1" << "name2");   (*)
>>>
>>>     This works fine. QThread freezes at (*) and waits for Process to finish.
>>>     Right after (*) I try to read all cout of process:
>>>         if( !nRESULT) {
>>>            QString oCout = poProcess->readAllStandardOutput();
>>>
>>>            std::cout << oCout.toStdString() << std::endl;   // Nothing
>>>     actually printed
>>>         }
>>>     But I am sure that scripts prints out some line with echo like:
>>>         echo "Hello world $1 $2"
>>>
>>>     Question: What am I doing wrong here?
>>>         
>>
>>     QProcess::execute() is a static function and will not change the state of your 
>>     poProcess instance. If you need to get the output from the process you need 
>>     to use QProcess::start() . For more info see the docs. 
>>
>>       
>>>     Thank you, Samvel.
>>>         
>>
>>
>>
>>       
>


Message 7 in thread

On Wednesday 15 August 2007, Samvel Khalatian wrote:
> Hmm, I don't think exec() should solve given problem. Exec() is a
> functions (the same idea as QApplication::exec() ):
>      You enter Event Loop and Exit it on exec() return. Thus if I write
> something like:
>
> void MyThread::run() {
> ...
> exec(); // Events processing within this function
> ...
> }
>
> BTW, everything works fine now, thanks to Iulian M notes. I have QHttp
> working with WEB resources. The only annoying problem left is that I
> always get a message like:
> QObject: can not kill timers
>
[snip]


You could install a custom message handler using qInstallMsgHandler and call 
abort() on any message so in the stack trace you can see where the message is 
coming from.

Also posting your thread's run() method ( and other relevant code ) might be 
usefull.

After a quick grep on the qt sources I suspect the message comes from stopping 
a timer from a different thread but without the source code i can't tell for 
sure.



-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 8 in thread

Hi,

After struggling with Qt Threads concept for couple last days I figured 
out finally what exactly was wrong in my program and why "Segmentation 
Fault"s took place quite often especially while trying to work with 
QHttp and QFile per thread.

I put a brief summary that might be useful for all other developers who 
is interested in working with QThreads (at least for me all this staff 
was not so obvious as it is now). Correct me if anything is missed.

Just follow several rules:

1. override QThread::run() method in order to have your code involved. 
Unfortunately the reason of my confusion was led by (inappropriate one 
to my mind) example taken from Chapter 18 of "C++ GUI Programming with 
Qt 4" - Multithreading. Here is a part of run() method put there:
void Thread::run() {
 ...
 while( !stopped) {
 ...
 }
 ...
}

Instead of having given artificial loop use QThread::exec(). In last 
case correct Events Handling loop will be involved and Signals<->Slots 
mechanism will work properly. Thus the idea is:

void Thread::run() {
  // Preparation for execution, e.g. opening files and so on
  exec(); // Enter Events Loop appropriate for given thread
  // Post-jog, e.g. closing files...
}

2. Whenever Class or function is specified as reentrant in Qt 
documentation - it can be used in normal way in Custom Thread unless 
several threads try to share object. In last case only Thread safe 
classes should be used.

3. Never create objects in Custom Thread on heap store sending this 
(pointer to current, child thread) as parent. Only Main Thread should be 
used as Parent in Qt.

IMHO, Those are 3 main points that deserve paying attention while 
working with QThreads.

Sincerely yours, Samvel.

--
 [ signature omitted ] 

Message 9 in thread

On Thursday 16 August 2007, Samvel Khalatian wrote:

[snip]

> 2. Whenever Class or function is specified as reentrant in Qt
> documentation - it can be used in normal way in Custom Thread unless
> several threads try to share object. In last case only Thread safe
> classes should be used.

In the last case you can use non thread safe types but you will need to 
protect access to them using QMutex & friends.

>
> 3. Never create objects in Custom Thread on heap store sending this
> (pointer to current, child thread) as parent. Only Main Thread should be
> used as Parent in Qt.

The only restriction in this case is that the parent has to live in the same 
thread as it's children. The (Q)Thread class does not live in the thread it 
manages so it can't be a parent of QObjects that live in that thread. 

>
> IMHO, Those are 3 main points that deserve paying attention while
> working with QThreads.
>

Even with the high-level api that qt (or boost) provides multithreading 
programming is a difficult and error prone task. Unless you have a very good 
understanding of all the concepts involved or are ready to spend A LOT of 
time finding strange errors and learning to use multithreading you are better 
off using event driven programming in single thread environment. 

Don't get me wrong, multithreading a very powerfull tool and its worth all the 
efforts of learning to use it, also there are cases where it will make a real 
difference in therms of performance and app design.

In conclusion I don't think that there are a set of rules that will spear you 
the frustrations of learning multithreading. 


-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 10 in thread

Honestly speaking I have never had experience working with threads 
before but it seems to be very nice and interesting topic. I'd like to 
improve my knowledge in current direction. What book(s) would you 
suggest to read about Threads in general and QThreads in particular?

Thank you.

Iulian M wrote:
> On Thursday 16 August 2007, Samvel Khalatian wrote:
>
> [snip]
>
>   
>> 2. Whenever Class or function is specified as reentrant in Qt
>> documentation - it can be used in normal way in Custom Thread unless
>> several threads try to share object. In last case only Thread safe
>> classes should be used.
>>     
>
> In the last case you can use non thread safe types but you will need to 
> protect access to them using QMutex & friends.
>
>   
>> 3. Never create objects in Custom Thread on heap store sending this
>> (pointer to current, child thread) as parent. Only Main Thread should be
>> used as Parent in Qt.
>>     
>
> The only restriction in this case is that the parent has to live in the same 
> thread as it's children. The (Q)Thread class does not live in the thread it 
> manages so it can't be a parent of QObjects that live in that thread. 
>
>   
>> IMHO, Those are 3 main points that deserve paying attention while
>> working with QThreads.
>>
>>     
>
> Even with the high-level api that qt (or boost) provides multithreading 
> programming is a difficult and error prone task. Unless you have a very good 
> understanding of all the concepts involved or are ready to spend A LOT of 
> time finding strange errors and learning to use multithreading you are better 
> off using event driven programming in single thread environment. 
>
> Don't get me wrong, multithreading a very powerfull tool and its worth all the 
> efforts of learning to use it, also there are cases where it will make a real 
> difference in therms of performance and app design.
>
> In conclusion I don't think that there are a set of rules that will spear you 
> the frustrations of learning multithreading. 
>
>
>   


Message 11 in thread

On Friday 17 August 2007, Samvel Khalatian wrote:
> Honestly speaking I have never had experience working with threads
> before but it seems to be very nice and interesting topic. I'd like to
> improve my knowledge in current direction. What book(s) would you
> suggest to read about Threads in general and QThreads in particular?

ATM the only thing i can recommend is http://boost.org/doc/html/thread.html , 
the documentation for the Boost.Thread library. There are a lot of concepts 
described there witch are independent of the boost library. Also there are 
some references to other books that are good. ( ex:  Pattern-Oriented 
Architecture )

I don't know of any writings that are specific to QThread, but i guess google 
can find a few writings about it.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.