// // mp::blocking_vector // // Copyright (C) 2008 FURUHASHI Sadayuki // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #ifndef MP_BLOCKING_VECTOR_H__ #define MP_BLOCKING_VECTOR_H__ #include "mp/pthread.h" #include namespace mp { template class blocking_vector { public: blocking_vector(); ~blocking_vector(); public: typedef T value_type; public: void push_back(const T& x); void swap(std::vector& x); private: std::vector m_vector; pthread_mutex m_mutex; pthread_cond m_cond; }; template blocking_vector::blocking_vector() { } template blocking_vector::~blocking_vector() { } template void blocking_vector::push_back(const T& x) { pthread_scoped_lock lk(m_mutex); m_vector.push_back(x); m_cond.signal(); } template void blocking_vector::swap(std::vector& x) { pthread_scoped_lock lk(m_mutex); while(m_vector.empty()) { m_cond.wait(m_mutex); } m_vector.swap(x); } } // namespace mp #endif /* mp/blocking_vector.h */