A 或 B 哪个更好或更快?
std::deque<Myclass> queue;
... // do something
std::size_t size = 0;
... // create n threads, one push queue and others pop queue.
// a thread do below
#ifdef A
pthread_rwlock_wrlock(&rwlock);
queue.push_front(myobj);
size = queue.size();
pthread_rwlock_unlock(&rwlock);
#endif
#ifdef B
pthread_rwlock_wrlock(&rwlock);
queue.push_front(myobj);
pthread_rwlock_unlock(&rwlock);
// if there is some operation,
// I think this B is better,
// because I should get the newest size.
pthread_rwlock_rdlock(&rwlock);
size = queue.size();
pthread_rwlock_unlock(&rwlock);
#endif
// other threads do below
pthread_rwlock_wrlock(&rwlock);
queue.pop_back();
pthread_rwlock_unlock(&rwlock);
那是我的不理解。
任何信息或建议对我都有很大的帮助!
为我糟糕的英语道歉!