Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Luca Risolia Newsgroups: comp.lang.c++,comp.programming.threads Subject: Re: Why is java consumer/producer so much faster than C++ Date: Mon, 23 Jul 2012 02:03:28 +0200 Organization: Aioe.org NNTP Server Lines: 26 Message-ID: References: NNTP-Posting-Host: Z2aAaWHo3goDty2MP7NWgA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.c++:17187 comp.programming.threads:963 On 23/07/2012 01:28, Melzzzzz wrote: > Still twice as fast as c++. > What it looks like, is that c++ spends much more time in syscalls > (futex) than java and that explains big difference. A lock-free "BlockingQueue" would probably be more efficient. You can also try to implement your Queue with std::array instead of std::deque and see how it performs. In the C++ version change: while (queue_.size() >= capacity_)c_full_.wait(lock); with c_full_.wait(lock, [this]{return queue_.size() < capacity_;}); and while (queue_.empty())c_empty_.wait(lock); with c_empty_.wait(lock, [this]{return !queue_.empty();}); Also use a std::lock_guard in empty() instead of std::unique_lock. The former is faster. (On a side note, the C++ version you have given does not have the best possible interface for a thread-safe queue)