简体   繁体   中英

In which thread are the slots executed?

Suppose I have four threads, with the following objects:

Thread 1: manages a boost::signals2 object. Call it s .

Thread 2: manages a X object and a reference to s . Call the X object o2 . The member function X::do() is connected to s , to be executed over o2 .

Thread 3: manages a Y object and a reference to s . Call the Y object o3 . The member function Y::do() is connected to s , to be executed over o3 .

Thread 4: manages a reference to s , and emit s the signal.

Where are the slots executed? All of them in thread 4, where the emit call took place? Over thread 1, because that's the thread of the signal's memory address? Or each slot is executed on its corresponding thread (2 and 3)?

The most obvious thing is the slots are executed over thread 4 , because there was were the signal was emitted (and the calls are synchonous), but it seems very weird to me a thread executing a function, or in general, manipulating an object, whose memory address belongs to another thread (for example, an object created on the heap of a thread, but being used in a different one).

Are the access to "foreigns" threads, at least, slower than just manipulating "my own" objects, or are there no differences at all?

Slots are invoked serially on the thread that invoked (emitted) the signal. For simplicity, you could think of signlas as simply a list of slots. Once aa signal is being invoked by some thread, it is as if that thread iterated on that list and invoked each connected slot.

Refering to your second question, a thread doesn't really 'own' what was allocated from it. Memory and object ownership is something you determine according to the way you manage your code. In general, any thread may access any memory address on its process memory space as long as you manage to pass it a valid address to that location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM