Vigo
Vigo

Reputation: 743

Suspend and resume one C++ thread in another

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;
}

Upvotes: 10

Views: 26143

Answers (2)

Gabriel
Gabriel

Reputation: 3604

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 :-)

Upvotes: 10

iain
iain

Reputation: 10928

Since you seem to be using win32 you can use an event

If you want to do something similar in pthreads there is an example here.

In both cases thread A will test a condition/event (and wait if it is set or continue if it is not) and thread B will set and clear the condition/event. Note you might also need a mutex between thread A and B depending on what they are doing, can they corrupt each other?

Upvotes: 0

Related Questions