[Interest] post event between threads

290 views
Skip to first unread message

Riccardo Roasio

unread,
Apr 2, 2012, 12:05:17 PM4/2/12
to inte...@qt-project.org
Hi,

how can i post an event from a thread to another?

my application have a thread that read from a serial port and another
thread that wait for something received on the serial port.

I cannot use postEvent because in one thread i don'e have reference to
the other...

How can i do that?

Thanks,
Riccardo
_______________________________________________
Interest mailing list
Inte...@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Samuel Gaist

unread,
Apr 2, 2012, 12:12:56 PM4/2/12
to Riccardo Roasio, inte...@qt-project.org
Hi,

If you just want to signal that something has arrived, why not simply use signals and slots ? They work across threads.

Hope this helps
Samuel

BOUCARD Olivier

unread,
Apr 2, 2012, 12:14:27 PM4/2/12
to inte...@qt-project.org
Hi Riccardo,

This kind of communication is done using a signal/slot.
This mechanism is thread-safe.

Olivier.


De : Riccardo Roasio <riccard...@gmail.com>
À : inte...@qt-project.org
Envoyé le : Lundi 2 avril 2012 18h05
Objet : [Interest] post event between threads

Andreas Pakulat

unread,
Apr 2, 2012, 12:20:10 PM4/2/12
to inte...@qt-project.org
On 02.04.12 18:05:17, Riccardo Roasio wrote:
> Hi,
>
> how can i post an event from a thread to another?
>
> my application have a thread that read from a serial port and another
> thread that wait for something received on the serial port.
>
> I cannot use postEvent because in one thread i don'e have reference to
> the other...

Then you cannot post events between the threads. You could just revert
to plain old function calls, ensuring proper locking.

Also note, to "post events into a thread" you actually need a QEventLoop
running in that thread.

Andreas

Thiago Macieira

unread,
Apr 2, 2012, 12:47:20 PM4/2/12
to inte...@qt-project.org
On segunda-feira, 2 de abril de 2012 18.05.17, Riccardo Roasio wrote:
> Hi,
>
> how can i post an event from a thread to another?
>
> my application have a thread that read from a serial port and another
> thread that wait for something received on the serial port.
>
> I cannot use postEvent because in one thread i don'e have reference to
> the other...
>
> How can i do that?

You don't post events to threads. You post events to objects only.

The events are delivered in each object's associated thread.

So, use:

QCoreApplication::postEvent(myObject, new MyEvent);

--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Intel Sweden AB - Registration Number: 556189-6027
Knarrarnäsgatan 15, 164 40 Kista, Stockholm, Sweden

signature.asc

Riccardo Roasio

unread,
Apr 3, 2012, 3:07:37 AM4/3/12
to Thiago Macieira, inte...@qt-project.org
I cannot use signal/slot because is blocking and i need to continue
reading from serial port while message is processing...

Ильгиз Магазов

unread,
Apr 3, 2012, 3:18:31 AM4/3/12
to inte...@qt-project.org
Hi, Riccardo

just use Qt::QueuedConnection, by default it is a Qt::AutoConnection

for me it works well,

http://qt-project.org/doc/qt-4.8/qobject.html#connect
http://qt-project.org/doc/qt-4.8/qt.html#ConnectionType-enum


Tue, 3 Apr 2012 09:07:37 +0200 от Riccardo Roasio <riccard...@gmail.com>:

Lincoln Ramsay

unread,
Apr 3, 2012, 3:19:41 AM4/3/12
to inte...@qt-project.org
On 04/03/2012 05:07 PM, ext Riccardo Roasio wrote:
> I cannot use signal/slot because is blocking and i need to continue
> reading from serial port while message is processing...

If you emit a signal and the receiver lives on the current thread and
the connect() call did not use Qt::QueuedConnection, then it is
"blocking". If you use Qt::DirectConnection or
Qt::BlockingQueuedConnection then it is always "blocking".

But if the receiver lives on another thread or if you specified
Qt::QueuedConnection then the signal puts an object on the event loop
and returns immediately.

--
Lincoln Ramsay - Senior Software Engineer
Qt Development Frameworks, Nokia - http://qt.nokia.com/

Riccardo Roasio

unread,
Apr 3, 2012, 3:29:55 AM4/3/12
to Lincoln Ramsay, inte...@qt-project.org
Ok,

i will try!

thanks everybody

Riccardo Roasio

unread,
Apr 3, 2012, 5:09:47 AM4/3/12
to Lincoln Ramsay, inte...@qt-project.org
I'm trying using signal/slots with Qt::QueuedConnection.
Now the problem is this one..i have

- gui thread (main)

- serial rx thread

- scriptmanager thread
|
|----- script runner 1 thread (started from scriptsmanager)
|
|----- script runner 2 thread (started from scriptsmanager)
|
|----- script runner N thread (started from scriptsmanager)

now the event is emitted from serial rx thread

the main thread is able to catch the signal
the scriptmanager is able to catch the signal
the scriptrunner is NOT able to catch the signal

the only difference is that the sciptrunner has no parent (in the
construtor the parent is NULL)

Riccardo

Samuel Gaist

unread,
Apr 3, 2012, 5:12:43 AM4/3/12
to interest@qt-project.org MailingList
If you show us the code you use to setup the connections, we might be able to offer better help.

Samuel

Riccardo Roasio

unread,
Apr 3, 2012, 5:19:18 AM4/3/12
to Samuel Gaist, interest@qt-project.org MailingList
Ok,

here serial rx threaad emit the signal:

emit MessageReceivedSignal();

here the main thread catch the signal:

connect(rxRoutine, SIGNAL(MessageReceivedSignal()),this,
SLOT(TestSlot()),Qt::QueuedConnection);

here is the constructor of scriptrunner that should catch the signal
but it doesn't

ScriptRunner::ScriptRunner(QObject *parent,Configuration *c,Script
*s,TxRoutine *t,RxRoutine *r,ComunicationLogConfigList *l,Message1281*
m) :
QThread(parent)
{
message1281=m;
comunicationLog=l;
activeConfiguration=c;
txRoutine=t;
rxRoutine=r;
myScript=s;
exit=false;

currentStep=0;

connect(rxRoutine, SIGNAL(MessageReceivedSignal()),this,
SLOT(TestSlot()),Qt::QueuedConnection);
}

and here is when i create the thread scriptrunner:

qDebug()<<"going to start... "<<tmpScript->GetName();
tmpRunner=new
ScriptRunner(0,activeConfiguration,tmpScript,txRoutine,rxRoutine,comunicationLog,message1281);
tmpScript->SetRunning(true);
tmpRunner->start();
runners.append(tmpRunner);

Giuseppe D'Angelo

unread,
Apr 3, 2012, 5:23:42 AM4/3/12
to Riccardo Roasio, interest@qt-project.org MailingList

Riccardo Roasio

unread,
Apr 3, 2012, 5:29:22 AM4/3/12
to Giuseppe D'Angelo, interest@qt-project.org MailingList
And so what i can do?
Or i have to read all the documentation....

Thiago Macieira

unread,
Apr 3, 2012, 9:08:18 AM4/3/12
to inte...@qt-project.org
On terça-feira, 3 de abril de 2012 11.29.22, Riccardo Roasio wrote:
> And so what i can do?
> Or i have to read all the documentation....

Add it to another class.

signature.asc

André Somers

unread,
Apr 3, 2012, 10:03:58 AM4/3/12
to inte...@qt-project.org
Op 3-4-2012 11:29, Riccardo Roasio schreef:

> And so what i can do?
> Or i have to read all the documentation....
Yes, reading the documentation will help you gain the needed
understanding. Threading is a complex topic with many pitfalls. You've
been given the list of things to read to understand this matter. I think
it's only fair to expect that now you go invest some time to go and
study those documents, before asking anyone here to invest more time to
help to to a solution that you don't understand.

André

Lincoln Ramsay

unread,
Apr 3, 2012, 8:38:13 PM4/3/12
to inte...@qt-project.org
On 04/03/2012 07:29 PM, ext Riccardo Roasio wrote:
> And so what i can do?

Read those docs.

In essence, you want something like this:

QThread *thread = new QThread;
MyObject *object = new MyObject;
object->moveToThread(thread);
thread->start();
connect(something, SIGNAL(blah()), object, SLOT(foo()));

--
Lincoln Ramsay - Senior Software Engineer
Qt Development Frameworks, Nokia - http://qt.nokia.com/

Reply all
Reply to author
Forward
0 new messages