-1

Assum have two thread that one of them has more priority and they are running on same core (single core), I only want work just one thread at same time.(maybe you say that is not threading paradigm , but actually I just made my problem minimal here)

T1 ~~~e1~e2~e3~e4~...~~~~~~~eK~~~~~~~~...~~~ eN~~~~~ ///(e1...eN)packed as a task.
                |            |
T2 ~~~~~~~~~~~~pause~~~~~~continue~~~~~~~~~~~~~~~~~~ ///(pause & continue)is just title time for T1 that T2 is operating (T2 has more priority).

**~** is time , and **e** is expressions that is evaluate there. and whole of e1,e2,... is one function that is api caller function(task) , So I want just pause T1 there(~pause~) and run my T2 until it's finished ,and when finished continue T1.

Note: I could not changed **e** job (function).

What I know?

creating conditional_variable(CV) and when finished T2 notify CV to wake up , but it is not my achievement because I want make T1 exactly pause in the e4(pause time) immediately and continue T2 until it's finished(or my continue time).

my knowledge is same as: https://en.cppreference.com/w/cpp/thread/condition_variable#Example Do we have any thread::method that pause immediately(force context switch)?(I dont mean yield!)

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • If you cannot modify `e()`, how do you intend the other thread to know when it has to pause? Where would you add the condition variable? – VLL Dec 16 '22 at 07:06
  • Ok , I want have other methods that is not using condition variable and achieve this goal: T2 tell T1 pause (dont use cpu cycles) , T2 tell T1 continue(using cycles now and using old memory , registers , ... ) , I mean force context switch in application level. Sorry if it's not completely guess. @VLL –  Dec 16 '22 at 07:09
  • 7
    There is absolutely no way whatsoever to pause another thread without it wanting to pause. – n. m. could be an AI Dec 16 '22 at 07:17
  • I guess you might get away with sending a signal to T2 and in the handler longjump to a loop waiting on a CV triggered by T1 later, then longjump back. But that is soo dirty. – Quimby Dec 16 '22 at 07:28
  • @Quimby Thanks for the hint , Can you explain more please(How can I do that, as you are saying?)? –  Dec 16 '22 at 07:37
  • I don't think there's any good way to unilaterally pause a thread, but if you can make the thread periodically lock a mutex and then immediately unlock it again, then to "pause" that thread, another thread merely needs to lock the same mutex and keep it locked until it's ready to "unpause" the thread again. – Jeremy Friesner Dec 16 '22 at 07:59
  • @JeremyFriesner Thanks for the guide, but it needs to modify a task function (packed task e in the example) that is not mine! in addition to check periods! , Am I right? Or do you mean to do something else? –  Dec 16 '22 at 08:05
  • 3
    You'll need to be able to modify the thread's code to modify the thread's behavior, I'm afraid. – Jeremy Friesner Dec 16 '22 at 08:09
  • 1
    @Quimby a longjmp in a C program is dirty... in a C++ program it's rather suicidal. And it's more like a kill than a pause. You can't longjmp back, longjmp can only jump up the call stack, not down. – n. m. could be an AI Dec 16 '22 at 08:10
  • Possible dupes https://stackoverflow.com/questions/9397068/how-to-pause-a-pthread-any-time-i-want/68119116#68119116 and https://stackoverflow.com/questions/54713131/how-to-pause-and-resume-thread and probably lots more. – n. m. could be an AI Dec 16 '22 at 08:16

2 Answers2

7

C++ has no way of facilitating such a thing. You don't have that level of control over threads.

On the OS level, that's what thread priorities are for. A higher-priority thread being ready should always be scheduled before a lower-priority thread. So you can use OS APIs to change the thread priorities for your threads, and should get approximately the right result - note, though, that this is approximate, because you have no guarantee the OS will immediately trigger a task switch on T2 getting ready.

Overall, the whole thing sounds like an XY problem, and you would be better served by looking at the underlying problem and thinking of better ways of solving it.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
2

I think you may be overthinking the problem you're trying to solve.

If you have (literally any) modern multitasking operating system (such as Linux, Windows, etc), and you make T2 a higher thread priority than T1, that operating system will preferentially run T2 ahead of T1. The only circumstance under which it will run T1 at all is if there is some reason for the OS to suspend T2, e.g. it's either got blocked on I/O, or it's waiting for a semaphore, etc. (if we're assuming a single core machine).

More or less the whole idea of a modern OS is to prevent the running of one thread or process influencing the running of another thread or process, unless they're written to cooperate somehow using synchronisation objects such as semaphores, condition variables, IPC, etc. So, you cannot achieve what you're wanting to do unless you can change e to use such mechanisms to allow T1 and T2 to cooperate. T2 will not be able to do it all by itself.

About the only thing that T2 can do is to use timers; i.e. it yields for a period of time, after which the OS will reschedule (resume) T2. But you can't get that aligned with the execution of e() in T1.

bazza
  • 7,580
  • 15
  • 22