pro*_*tec 6 linux condition-variable c++11
我正在使用在 ARM(而不是英特尔处理器)上运行的代码。从http://www.cplusplus.com/reference/condition_variable/condition_variable/wait_for/运行 c++11 代码示例(代码 A)来测试 wait_for() 机制。这不起作用 - 看起来 wait_for() 不等待。在英特尔工作得很好。经过一些研究并直接使用 pthread 库并设置 MONOTONIC_CLOCK 定义,解决了问题(代码 B)。(在ARM上运行不是问题)
我的问题是:如何强制 C++11 API wait_for() 与 MONOTONIC_CLOCK 一起使用?
实际上我想保留“CODE A”,但需要 MONOTONIC_CLOCK 的支持或设置。谢谢
// condition_variable::wait_for example
#include <iostream> // std::cout
#include <thread> // std::thread
#include <chrono> // std::chrono::seconds
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable, std::cv_status
std::condition_variable cv;
int value;
void read_value() {
std::cin >> value;
cv.notify_one();
}
int main ()
{
std::cout << "Please, enter an integer (I'll be printing dots): \n";
std::thread th (read_value);
std::mutex mtx;
std::unique_lock<std::mutex> lck(mtx);
while
(cv.wait_for(lck,std::chrono::seconds(1))==std::cv_status::timeout)
{
std::cout << '.' << std::endl;
}
std::cout << "You entered: " << value << '\n';
th.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <sys/time.h>
#include <unistd.h>
#include <iostream> // std::cout
#include <thread> // std::thread
#include <chrono> // std::chrono::seconds
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable, std::cv_status
const size_t NUMTHREADS = 1;
pthread_mutex_t mutex;
pthread_cond_t cond;
int value;
bool done = false;
void* read_value( void* id )
{
const int myid = (long)id; // force the pointer to be a 64bit integer
std::cin >> value;
done = true;
printf( "[thread %d] done is now %d. Signalling cond.\n", myid, done
);
pthread_cond_signal( &cond );
}
int main ()
{
struct timeval now;
pthread_mutexattr_t Attr;
pthread_mutexattr_init(&Attr);
pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, &Attr);
pthread_condattr_t CaAttr;
pthread_condattr_init(&CaAttr);
pthread_condattr_setclock(&CaAttr, CLOCK_MONOTONIC);
pthread_cond_init(&cond, &CaAttr);
std::cout << "Please, enter an integer:\n";
pthread_t threads[NUMTHREADS];
int t = 0;
pthread_create( &threads[t], NULL, read_value, (void*)(long)t );
struct timespec ts;
pthread_mutex_lock( &mutex );
int rt = 0;
while( !done )
{
clock_gettime(CLOCK_MONOTONIC, &ts);
ts.tv_sec += 1;
rt = pthread_cond_timedwait( & cond, & mutex, &ts );
std::cout << "..." << std::endl;
}
pthread_mutex_unlock( & mutex );
std::cout << "You entered: " << value << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
std::condition_variable::wait_for的文档说:
使用稳定的时钟来测量持续时间。
类
std::chrono::steady_clock代表一个单调时钟。这个时钟的时间点不能随着物理时间的前进而减少。
不幸的是,这是 gcc Bug 41861 (DR887) - (DR 887)(C++0x) 不使用它system_clock用来代替steady_clock条件变量的 motononic_clock 。
一种解决方案是使用wait_until(请务必阅读注释部分)函数,该函数允许指定相对于特定时钟的持续时间。例如:
cv.wait_until(lck, std::chrono::steady_clock::now() + std::chrono::seconds(1))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3019 次 |
| 最近记录: |