Files
esp-idf/components/pthread/test/test_pthread_cxx.cpp

50 lines
1.4 KiB
C++
Raw Normal View History

2017-08-24 02:53:20 +03:00
#include <iostream>
#include <thread>
#include <mutex>
#include "unity.h"
std::shared_ptr<int> global_sp;
std::mutex mtx;
static void thread_main() {
2017-08-24 02:53:20 +03:00
int i = 0;
std::cout << "thread_main CXX " << std::hex << std::this_thread::get_id() << std::endl;
while (i < 10) {
// mux test
mtx.lock();
// yield test
std::this_thread::yield();
(*global_sp)++;
mtx.unlock();
std::cout << "thread " << std::hex << std::this_thread::get_id() << ": " << i++ << " val= " << *global_sp << std::endl;
// sleep_for test
std::chrono::milliseconds dur(300);
std::this_thread::sleep_for(dur);
// sleep_until test
using std::chrono::system_clock;
std::time_t tt = system_clock::to_time_t(system_clock::now());
struct std::tm *ptm = std::localtime(&tt);
ptm->tm_sec = 1;
std::this_thread::sleep_until(system_clock::from_time_t (mktime(ptm)));
2017-08-24 02:53:20 +03:00
}
}
TEST_CASE("pthread CXX test 1", "[pthread]")
{
std::cout << "TEST START!" << std::endl;
2017-08-24 02:53:20 +03:00
global_sp.reset(new int(1));
std::thread t1(thread_main);
std::thread t2(thread_main);
if (t1.joinable()) {
std::cout << "Join thread " << std::hex << t1.get_id() << std::endl;
t1.join();
}
if (t2.joinable()) {
std::cout << "Join thread " << std::hex << t2.get_id() << std::endl;
t2.join();
}
std::cout << "TEST END!" << std::endl;
2017-08-24 02:53:20 +03:00
}