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 <iostream> |
||||||
#include <mutex> |
|
||||||
#include <string> |
|
||||||
#include <thread> |
#include <thread> |
||||||
|
using namespace std::chrono_literals; |
||||||
|
|
||||||
std::mutex m; |
int main() |
||||||
std::condition_variable cv; |
|
||||||
std::string data; |
|
||||||
bool ready = false; |
|
||||||
bool processed = false; |
|
||||||
|
|
||||||
void worker_thread() |
|
||||||
{ |
{ |
||||||
// wait until main() sends data
|
std::future<int> future = std::async(std::launch::async, []() |
||||||
std::unique_lock lk(m); |
{ |
||||||
cv.wait(lk, []{ return ready; }); |
std::this_thread::sleep_for(3s); |
||||||
|
return 8; |
||||||
// 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
|
std::cout << "waiting...\n"; |
||||||
// the waiting thread only to block again (see notify_one for details)
|
std::future_status status; |
||||||
lk.unlock(); |
|
||||||
cv.notify_one(); |
|
||||||
} |
|
||||||
|
|
||||||
int main() |
do |
||||||
{ |
{ |
||||||
std::thread worker(worker_thread); |
status = future.wait_for(100ms); |
||||||
|
switch (status) |
||||||
data = "Example data"; |
|
||||||
// send data to the worker thread
|
|
||||||
{ |
{ |
||||||
std::lock_guard lk(m); |
case std::future_status::deferred: |
||||||
ready = true; |
std::cout << "deferred\n"; |
||||||
std::cout << "main() signals data ready for processing\n"; |
break; |
||||||
|
case std::future_status::timeout: |
||||||
|
std::cout << "timeout\n"; |
||||||
|
break; |
||||||
|
case std::future_status::ready: |
||||||
|
std::cout << "ready!\n"; |
||||||
|
break; |
||||||
} |
} |
||||||
cv.notify_one(); |
|
||||||
|
|
||||||
// wait for the worker
|
|
||||||
{ |
|
||||||
std::unique_lock lk(m); |
|
||||||
cv.wait(lk, []{ return processed; }); |
|
||||||
} |
} |
||||||
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