mirror of
https://github.com/espressif/esp-idf.git
synced 2025-12-10 09:55:47 +00:00
pthread: Added support for pthread condition variables
This is required for std::condition_variable support Signed-off-by: Amey Inamdar <amey.inamdar@gmail.com>
This commit is contained in:
committed by
Ivan Grokhotkov
parent
597ce3b800
commit
edb2400742
48
components/pthread/test/test_cxx_cond_var.cpp
Normal file
48
components/pthread/test/test_cxx_cond_var.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include "unity.h"
|
||||
|
||||
#if __GTHREADS && __GTHREADS_CXX0X
|
||||
|
||||
std::condition_variable cv;
|
||||
std::mutex cv_m;
|
||||
std::atomic<int> i{0};
|
||||
|
||||
static void waits(int idx, int timeout_ms)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(cv_m);
|
||||
auto now = std::chrono::system_clock::now();
|
||||
|
||||
if(cv.wait_until(lk, now + std::chrono::milliseconds(timeout_ms), [](){return i == 1;}))
|
||||
std::cout << "Thread " << idx << " finished waiting. i == " << i << '\n';
|
||||
else
|
||||
std::cout << "Thread " << idx << " timed out. i == " << i << '\n';
|
||||
}
|
||||
|
||||
static void signals(int signal_ms)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(signal_ms));
|
||||
std::cout << "Notifying...\n";
|
||||
cv.notify_all();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(signal_ms));
|
||||
i = 1;
|
||||
std::cout << "Notifying again...\n";
|
||||
cv.notify_all();
|
||||
}
|
||||
|
||||
TEST_CASE("C++ condition_variable", "[std::condition_variable]")
|
||||
{
|
||||
i = 0;
|
||||
std::thread t1(waits, 1, 100), t2(waits, 2, 800), t3(signals, 200);
|
||||
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
|
||||
std::cout << "All threads joined\n";
|
||||
}
|
||||
#endif
|
||||
31
components/pthread/test/test_cxx_std_future.cpp
Normal file
31
components/pthread/test/test_cxx_std_future.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <future>
|
||||
#include <thread>
|
||||
#include "unity.h"
|
||||
|
||||
#if __GTHREADS && __GTHREADS_CXX0X
|
||||
TEST_CASE("C++ future", "[std::future]")
|
||||
{
|
||||
// future from a packaged_task
|
||||
std::packaged_task<int()> task([]{ return 7; }); // wrap the function
|
||||
std::future<int> f1 = task.get_future(); // get a future
|
||||
std::thread t(std::move(task)); // launch on a thread
|
||||
|
||||
// future from an async()
|
||||
std::future<int> f2 = std::async(std::launch::async, []{ return 8; });
|
||||
|
||||
// future from a promise
|
||||
std::promise<int> p;
|
||||
std::future<int> f3 = p.get_future();
|
||||
std::thread( [&p]{ p.set_value_at_thread_exit(9); }).detach();
|
||||
|
||||
std::cout << "Waiting..." << std::flush;
|
||||
f1.wait();
|
||||
f2.wait();
|
||||
f3.wait();
|
||||
std::cout << "Done!\nResults are: "
|
||||
<< f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n';
|
||||
t.join();
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user