Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to the multi threaded  programming . there is a requirement some thing like this.Suppose I have a button (play and Pause) play means its continues play going on or pause means it have to pause
Posted

POSIX threads are well documented. See for instance "POSIX Threads Programming"[^], possibly condition variables fit your needs.
 
Share this answer
 
Typically what you would want to do is check a variable/condition within your new thread and act accordingly.

C++
//pseudo-code
thread_executor(...)
{
  while(run){
    if(pause) continue;
    else{ 
       //do work...
    }
  }
}


There are some thread implementations that allow for external suspension of a thread but its not a recommended or ideal practice because it leaves the thread memory and accessors in an undefined state (i.e. what happens to the variables that you're using within that thread? what if you locked a shared memory location with a mutex?).


edit: There are some thread implementations (can't recall which one off the top of my head) that would lead a "tight" loop into taking up a lot of processor time (i.e. the continue statement above). In those cases, you can also save some cycles by sleeping for some period of time (i.e. if(pause) sleep(10)). Alternatively, you can use a control signal (in Windows, look up WaitForSingleObject()), but be sure to use timeouts so you don't lock up threads.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900