Thread: Can someone explain simultaneous multithreading to me?

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Can someone explain simultaneous multithreading to me?

    I'm trying to read the wiki and I think I'm just getting lost.

    From what I can tell or from a very simple version of what I read, SMT was the hardware's ability to run one thread on top of a stalled thread that is currently waiting for memory to be bused from the pool, for example.

    But when I read the wiki, I think I get a little confused.

    Does anyone have a good explanation?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    In "regular" multi-threading, the operating system quickly switches between threads by swapping the control state of the CPU. Each thread has its own set of registers, virtual memory, and execution privileges. When one thread is made inactive so another can run, this state is saved somewhere in memory to be restored later.

    So far so good?

    Now, imagine that the CPU itself can manage multiple sets of state. It has multiple copies of the registers and other data which describe the currently executing thread. In addition, the state of the instruction decoder and execution pipelines are physically duplicated as well. Now, the CPU can switch between these threading contexts itself, even without help from the operating system.

    It's not really different than software threading, it's simply hardware-assisted.

    The question is why we do this, when we already have software threading. The answer is the incredible speed at which threads can be switched in hardware. A software thread switch might take on the order of thousands of CPU cycles. A hardware thread switch might take as little as just one cycle.

    Once you have the ability to switch threads which such blinding speed, you can increase the efficiency of use of the different execution units on the CPU. If one thread is stalled for dozens of cycles doing a memory fetch, another thread can swoop in quickly and use that time to perform several integer division instructions (for instance). When the resource becomes unblocked the CPU can quickly switch back to the original thread.

    Basically, it's a way to interleave threading at a finer level than software could ever hope to achieve, with the end goal being more efficient use of the execution units.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by brewbuck View Post
    Each thread has its own set of registers, virtual memory, and execution privileges.
    At least in the case of Windows, all the threads in a process share the same virtual memory address space, and I think also the execution priviliges.

    Looking at the wiki article, SMT is similar to Intel's hyper-threading, where a single core's hyper-threading features are used to run two threads. This is different than using multiple cores to run multiple threads of a process which the wiki article refers to as symmetric multi-processing. In the case of Windows, a multi-threaded process will use multiple cores if they are available.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rcgldr View Post
    At least in the case of Windows, all the threads in a process share the same virtual memory address space, and I think also the execution priviliges.
    Yes, from the OS perspective. From the hardware's perspective, each thread is independent, only some of them happen to share the same page tables. The CPU doesn't care one way or the other.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing n simultaneous conditions
    By degant7 in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2011, 08:24 PM
  2. Simultaneous Tasks
    By C_ntua in forum C# Programming
    Replies: 2
    Last Post: 07-28-2009, 08:21 AM
  3. Simultaneous linear Equations
    By dvd4alll in forum C# Programming
    Replies: 8
    Last Post: 02-08-2008, 07:11 PM
  4. Simultaneous processes
    By Lateralus in forum C Programming
    Replies: 5
    Last Post: 06-30-2005, 07:58 PM
  5. simultaneous equations
    By nic_elmo_13 in forum C Programming
    Replies: 3
    Last Post: 04-14-2003, 08:11 AM