Path: csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Melzzzzz Newsgroups: comp.lang.c++,comp.programming.threads Subject: Re: Why is java consumer/producer so much faster than C++ Date: Mon, 23 Jul 2012 12:17:46 +0200 Organization: albasani.net Lines: 44 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net Hxpjf1liurg9gunfaMSLzZM4eRdYLagomgiXDRBycK8OfF+9paN4JcwcVJ+yG7qhqPh3C+OSFXunGYlimKAYNVVhrTGHbk8CByCJipMIyBkRkduCUpIkTPsjgl0bvE8x NNTP-Posting-Date: Mon, 23 Jul 2012 10:17:46 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="FGtVFaxwTub+LhEUOhDGO1R94RMkPASindagGu17qpWlqNVHuSZaN0jgaZ9M/6M/yygV7BNKzXHNAoNQiALkvrw3zaSdC38hTEQzQqhJqEOAJTRQXLNZkH4wytk7EGRf"; mail-complaints-to="abuse@albasani.net" X-Newsreader: Claws Mail 3.8.0 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Cancel-Lock: sha1:XM+muj4qkP1v7B9AbRwUYBRph+A= Xref: csiph.com comp.lang.c++:17195 comp.programming.threads:966 On Mon, 23 Jul 2012 02:03:28 +0200 Luca Risolia wrote: > 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. I will try. Seems that java ArrayBlockingQueue is more efficient because if that. > > 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. Yes, I got speed up of few seconds, thanks. > > (On a side note, the C++ version you have given does not have the > best possible interface for a thread-safe queue) I have just copied Java BlockingQueue interface, which is of course suited for Java. Don't know actually how ideal interface would look like. >