Linux Threads suspend/resume

后端 未结 9 729
情歌与酒
情歌与酒 2020-12-13 11:10

I\'m writing a code in which I have two threads running in parallel.

1st is the main thread which started the 2nd thread. 2nd thread is just a simple thread executin

9条回答
  •  猫巷女王i
    2020-12-13 11:50

    Not sure if you will like my answer or not. But you can achieve it this way.

    If it is a separate process instead of a thread, I have a solution(This might even work for thread, maybe someone can share your thoughts) using signals.

    There is no system currently in place to pause or resume the execution of the processes. But surely you can build one.

    Steps i would do if i want it in my project:

    • Register a signal handler for the second process.

    • Inside the signal handler, wait for a semaphore.

    • Whenever you want to pause the other process, just send in a signal
      that you registered the other process with. The program will go into sleep state.

    • When you want to resume the process, you can send a different signal again. Inside that signal handler, you will check if the semaphore is locked or not. If it is locked, you will release the semaphore. So
      the process 2 will continue its execution.

    If you can implement this, please do share your feedack, if it worked for you or not. Thanks.

提交回复
热议问题