Mohsin
Mohsin

Reputation: 351

c++ - how to know when the thread context switch happens?

Like the title says how do we know when does the context switch happens when using std::threads in c++

for example

void a()
{
   //some code
}                                    
void b(); 
{
   //some code 
}                                    

int main ()
{
  thread th1 (a);       
  thread th2 (b); 

  th1.join();                                        
  th2.join();                                       

  return 0;
} 

is there a way to know when the O.S makes the thread switch and we could actually depict like print a or b when the respective function is processing.

Note: I've used mutexes and control variables putting a simple cout<<"a is being processed" on them doesn't necessarily depict the real context switch.

Upvotes: 3

Views: 6233

Answers (4)

Riot
Riot

Reputation: 16716

Aside from the more abstract answers above, on POSIX systems, you can find out which core you're currently executing on with sched_getcpu()

#include <sched.h>

int current_cpu = sched_getcpu();

By monitoring this and observing changes, you can detect when your process has been moved between cores. This can be useful in conjunction with setting cpu affinity. However, it won't give any useful information if your process has been suspended and resumed on the same core.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490308

There may not be a context switch to know about.

On a machine with two (or more) cores, both your threads may be running simultaneously, with no context switches happening at all until they finish.

Keep mind that most current machines have at least two cores, and machines with 4 to 8 cores are already fairly common. Assuming that code like this will lead to any context switches is basically obsolete. In theory it's been obsolete for a long time (i.e., there have been parallel machines for decades).

Now it's pretty thoroughly obsolete in practical fact--even if you get your new computers by scrounging through junk that others have given away or donated, chances are still pretty good that their old junk will have at least two cores, and four isn't out of the question at all.

Upvotes: 3

Adrian Colomitchi
Adrian Colomitchi

Reputation: 3992

we could actually depict like print a or b when the respective function is processing.

If this is meant only for demo/teaching purposes

Let me use this metaphor: you have a black-box controller (the OS) that pushes current through two diodes (threads). Since you don't have any command over the controller, the only way to see which path your current flows is to replace your diodes by LED-es - in all moments time the current flow through them, they emit light. (note: as a LED may not be equivalent with the diode it replaces, you wouldn't want to sell the product which may blow up the magic smoke - that is to say "Don't use this technique outside diagnosis cases inside your lab")

In terms of your a() and b() functions, this translates into making them "emitting light" at every instruction they perform (or just enough of those instructions to achieve the demoed purpose).

So write a macro that wraps those instructions and, apart from having the instruction executed, they have a side effect you can use to detect which of the threads is running. Mind you: they can run in the same time on two different CPU cores.


The technique is usually called "instrumentation" and it is used (adapted for their particular cases) by, for example, performance evaluation tools.

Upvotes: 1

Jason C
Jason C

Reputation: 40356

You can't know and you have no reason to know.

One of the driving principles behind kernel / hardware thread and context management is specifically making this whole process invisible to you. There might not even be any context switches between your two threads (other threads on the system aside), if both are running on different cores.

If you're in a situation where you think you need to do this, you probably need to sit back and rethink your design, and familiarize yourself with the various synchronization constructs that are generally available for you to use.

Upvotes: 0

Related Questions