Search code examples
c++multithreadingsequential

What is an inter-thread operation


Ive been reading a book about multithreading in c++, and i've got really confused on what does the term inter-thread actually means. At first i thought that it was used to describe between threads communication. But then as the book advances further and further, it starts to use this term more loosely to the point where i dont understand what it means anymore. For example take this short paragraph

"Inter-thread happens-before also combines with the sequenced-before relation: if operation A is sequenced before operation B, and operation B inter-thread happens- before operation C, then A inter-thread happens-before C. Similarly, if A synchronizes- with B and B is sequenced before C, then A inter-thread happens-before C. These two together mean that if you make a series of changes to data in a single thread, you need only one synchronizes-with relationship for the data to be visible to subsequent opera- tions on the thread that executed C." null

After reading that paragraph i was officially lost. And the book, which have have been very good at explaining new terms, completey forgot about this one. Which makes me think that it was such a commonly used term that didn't need to be explained, which made asking this question all the more difficult. So could someone take the time to explain what this term actually means?


Solution

  • The term inter-thread in that context is not bound to a specific channel or metodology of communication between threads but rather to the concept of happens-before.

    Let's take for instance the passage you cited:

    if operation A is sequenced before operation B, and operation B inter-thread happens-before operation C, then A inter-thread happens-before C

    in the following code

    thread1
    --------------
    0x10: int a = 1;
    0x11: sendResultToThread(a);
    
    
    thread2
    --------------
    0x12: int c = getResultFromOtherThread();
    

    simplifying with no data races and assuming atomicity for the instructions here involved, the sentence says that if operation 0x10 is sequenced before 0x11 (and in the same thread it is, since it comes before in the source code) and operation 0x11 happens-before in a multi-threaded context (i.e. there's some kind of mechanism for which the memory effects made by 0x11 are visible to 0x12 before 0x12 is executed and also assures, among the other things, that 0x11 will always be temporally executed before 0x12 when multiple threads interact), then 0x10 inter-thread happens-before C.

    Similar conclusions can be drawn for the second sentence.

    So to answer your question: an inter-thread operation (not to be confused with intra-thread i.e. in the same thread) refers to the interaction and synchronization (either explicit or implicit) between threads. Don't think of it in terms of shared memory areas or the like since it might hinder your reasoning on the concepts explained.