Now using a std::async and future to do an async popen but I also need the FILE read in read_line to be async, so now I'm at a point where I have to refactor into a better statemachine.
parent
501cb5fe25
commit
356314406f
@ -1,54 +1,37 @@ |
||||
#include <condition_variable> |
||||
#include <chrono> |
||||
#include <future> |
||||
#include <iostream> |
||||
#include <mutex> |
||||
#include <string> |
||||
#include <thread> |
||||
|
||||
std::mutex m; |
||||
std::condition_variable cv; |
||||
std::string data; |
||||
bool ready = false; |
||||
bool processed = false; |
||||
|
||||
void worker_thread() |
||||
{ |
||||
// wait until main() sends data
|
||||
std::unique_lock lk(m); |
||||
cv.wait(lk, []{ return ready; }); |
||||
|
||||
// after the wait, we own the lock
|
||||
std::cout << "Worker thread is processing data\n"; |
||||
data += " after processing"; |
||||
|
||||
// send data back to main()
|
||||
processed = true; |
||||
std::cout << "Worker thread signals data processing completed\n"; |
||||
|
||||
// manual unlocking is done before notifying, to avoid waking up
|
||||
// the waiting thread only to block again (see notify_one for details)
|
||||
lk.unlock(); |
||||
cv.notify_one(); |
||||
} |
||||
using namespace std::chrono_literals; |
||||
|
||||
int main() |
||||
{ |
||||
std::thread worker(worker_thread); |
||||
|
||||
data = "Example data"; |
||||
// send data to the worker thread
|
||||
std::future<int> future = std::async(std::launch::async, []() |
||||
{ |
||||
std::lock_guard lk(m); |
||||
ready = true; |
||||
std::cout << "main() signals data ready for processing\n"; |
||||
} |
||||
cv.notify_one(); |
||||
std::this_thread::sleep_for(3s); |
||||
return 8; |
||||
}); |
||||
|
||||
std::cout << "waiting...\n"; |
||||
std::future_status status; |
||||
|
||||
// wait for the worker
|
||||
do |
||||
{ |
||||
std::unique_lock lk(m); |
||||
cv.wait(lk, []{ return processed; }); |
||||
status = future.wait_for(100ms); |
||||
switch (status) |
||||
{ |
||||
case std::future_status::deferred: |
||||
std::cout << "deferred\n"; |
||||
break; |
||||
case std::future_status::timeout: |
||||
std::cout << "timeout\n"; |
||||
break; |
||||
case std::future_status::ready: |
||||
std::cout << "ready!\n"; |
||||
break; |
||||
} |
||||
} |
||||
std::cout << "Back in main(), data = " << data << '\n'; |
||||
while (status != std::future_status::ready); |
||||
|
||||
worker.join(); |
||||
std::cout << "result is " << future.get() << '\n'; |
||||
} |
||||
|
Loading…
Reference in new issue