On linux, the most reliable way I've found to identify process context switching, is through the command:
pidstat -hluwrt | grep "processname"
The 'tid' column (#3) is the same as 'gettid()', thus allowing the developer to directly correlate which thread is using CPU and context switching. I suggest that when a thread is spawned for the program to spit out the gettid() value: printf("%lul",gettid()).
The last 2 columns, prior to process command line, are the 'cswtch/s' (voluntary) and 'nvcswtch/s' (non-voluntary) context switch counts, per second.
When the 'cswtch/s' is high (1000's) your process is is cycling through the 'wake' and 'sleep' excessively. You may want to consider some kind of buffer to supply the threads, allowing for longer awake & sleep times. ex: When buffer NOT full the thread stays asleep longer. When the buffer becomes full, the thread is awake until the buffer becomes empty.
When the 'nvswtch/s' is high (1000's), this is a symptom your system is heavily loaded and the individual thread is contending for CPU time. You may want to investigate the server load, quantity of active processes & threads on the server: 'top' or 'htop' are your friends.
I find the following script useful debugging/optimizing process threading (outputs every 20 seconds):
stdbuf -oL pidstat -hluwrt 20 | stdbuf -oL grep -e "processname" -e "^#"
Documentation for gettid: (doc here)
Documentation for pidstat: (doc here)
Documentation for stdbuf: (doc here)