I have a C++ code with two threads in it. After an event 'A' in thread 2, thread 1 should be paused(suspended), some more tasks are to be executed in thread 2 (say event 'B')and finally thread 1 should be resumed. Is there any way to do this?
My code looks something like this:
HANDLE C;
DWORD WINAPI A (LPVOID in)
{
while(1){
// some operation
}
return 0;
}
DWORD WINAPI B (LPVOID in)
{
while(1){
//Event A occurs here
SuspendThread (C);
//Event B occurs here
ResumeThread (C);
}
return 0;
}
int main()
{
C = CreateThread (NULL, 0, A, NULL, 0, NULL);
CreateThread (NULL, 0, B, NULL, 0, NULL);
return 0;
}
suspend(): This is a method used to suspend the thread. The thread will remain suspended and won't perform its tasks until it is resumed. resume(): This is a method used to resume the suspended thread.
In this case, the suspend() method allows a thread to temporarily cease executing. resume() method allows the suspended thread to start again.
The suspend() method of thread class puts the thread from running to waiting state. This method is used if you want to stop the thread execution and start it again when a certain event occurs. This method allows a thread to temporarily cease execution. The suspended thread can be resumed using the resume() method.
Resume(); to suspend and resume the work done by the thread. But Now while compiling it says Thread. Suspend(); And Thread. Resume(); are obsolete and deprecated.
Hi I have an example that I got inspired from cpp reference. The example uses C++ 11 so given that this question was not flagged win32 but c++ and multithreading I post something.
First here is the original link.
http://en.cppreference.com/w/cpp/thread/sleep_for
Now here is the code I got that solves the problem.
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex m;
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;
void ThreadB_Activity()
{
// Wait until ThreadA() sends data
{
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{return ready;});
}
std::cout << "Thread B is processing data\n";
data += " after processing";
// Send data back to ThreadA through the condition variable
{
std::lock_guard<std::mutex> lk(m);
processed = true;
std::cout << "Thread B signals data processing completed\n";
}
cv.notify_one();
}
void ThreadA_Activity()
{
std::cout<<"Thread A started "<<std::endl;
data = "Example data";
// send data to the worker thread
{
std::lock_guard<std::mutex> lk(m);
ready = true;
std::cout << "Thread A signals data are ready to be processed\n";
}
cv.notify_one();//notify to ThreadB that he can start doing his job
// wait for the Thread B
{
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{return processed;});
}
std::cout << "Back in Thread A , data = " << data << '\n';
std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ));
std::cout<<"end of Thread A"<<std::endl;
}
int main()
{
std::thread ThreadB(ThreadB_Activity);
std::thread ThreadA(ThreadA_Activity);
ThreadB.join();
ThreadA.join();
std::cout << "Back in main , data = " << data << '\n';
return 0;
}
Hope that helps, any comments are welcome :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With