Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #17266
| From | Luca Risolia <luca.risolia@studio.unibo.it> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Why is java consumer/producer so much faster than C++ |
| Date | 2012-07-26 21:19 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <jus57m$2e5$1@speranza.aioe.org> (permalink) |
| References | (2 earlier) <jujut6$p5s$1@news.albasani.net> <86c79114-84e6-4065-a9a4-b5b3edf8fcac@googlegroups.com> <jukjda$jj5$1@news.albasani.net> <6c287737-09a9-44f5-ba15-eb28921af8a7@googlegroups.com> <jumu0i$luo$1@news.albasani.net> |
On 24/07/2012 21:45, Melzzzzz wrote:
> This is version with array (identical implementation
> as ArrayBlockingQueue in Java), instead of deque (I
> used atomic type for count_). This executes about same speed as Java
> as atomic type slows it down a bit.
> Sadly, no noticing difference between deque and array.
Right, it may well depend on how smart the std::deque::allocator_type is.
Below is a version of the BlockingQueue using spin locks. Compared to
the original version in Java it is about 2.5 times faster on my
platform, provided that you remove std::rand() from the test, as it
seems to be a real bottleneck and does not give any precious
informations about the efficiency of the Queue (which is the thing to
measure after all).
Let me know your results.
---
#include <thread>
#include <cstdlib>
#include <functional>
#include <atomic>
#include <memory>
#include <type_traits>
class spinlock_mutex {
std::atomic_flag flag = ATOMIC_FLAG_INIT;
public:
void lock() {
while (flag.test_and_set(std::memory_order_acquire)) {
std::this_thread::yield();
};
}
void unlock() {
flag.clear(std::memory_order_release);
}
};
template <class T, class A = std::allocator<T >>
class BlockingQueue {
const std::size_t cap_;
A alloc_;
T* queue_;
std::size_t n = 0, b = 0, f = 0;
spinlock_mutex m_;
public:
BlockingQueue(const std::size_t cap, const A& alloc = A())
: cap_(cap), alloc_(alloc), queue_(alloc_.allocate(cap)) { }
~BlockingQueue() {
alloc_.deallocate(queue_, cap_);
}
static_assert(std::is_nothrow_move_constructible<T>::value,
"BlockingQueue cannot be exception-safe");
void put(const T t) {
for (;;) {
m_.lock();
if (n < cap_)
break;
m_.unlock();
}
alloc_.construct(&queue_[f], std::move(t));
f = ++f % cap_;
++n;
m_.unlock();
}
T take() {
for (;;) {
m_.lock();
if (n > 0)
break;
m_.unlock();
}
const T t = std::move(queue_[b]);
alloc_.destroy(&queue_[b]);
b = ++b % cap_;
--n;
m_.unlock();
return t;
}
};
int main() {
BlockingQueue<int> produced(100000);
const int nitems = 100000000;
std::srand(12345);
std::function<void() > f_prod = [&](){
int i = nitems;
while (i-- > 0) {
produced.put(i);
}
};
std::thread producer1(f_prod);
std::function<void() > f_cons = [&](){
const int size = 10000;
int arr[size];
int i = nitems;
while (i-- > 0) {
arr[std::rand() % size] = produced.take();
}
};
std::thread consumer1(f_cons);
producer1.join();
consumer1.join();
}
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar
Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-22 23:59 +0200
Re: Why is java consumer/producer so much faster than C++ Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-22 15:42 -0700
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-23 01:28 +0200
Re: Why is java consumer/producer so much faster than C++ Luca Risolia <luca.risolia@studio.unibo.it> - 2012-07-23 02:03 +0200
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-23 12:17 +0200
Re: Why is java consumer/producer so much faster than C++ Luca Risolia <luca.risolia@studio.unibo.it> - 2012-07-24 00:33 +0200
Re: Why is java consumer/producer so much faster than C++ Bo Persson <bop@gmb.dk> - 2012-07-24 19:50 +0200
Re: Why is java consumer/producer so much faster than C++ Juha Nieminen <nospam@thanks.invalid> - 2012-07-23 06:37 +0000
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-23 12:33 +0200
Re: Why is java consumer/producer so much faster than C++ Juha Nieminen <nospam@thanks.invalid> - 2012-07-23 11:46 +0000
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-23 15:33 +0200
Re: Why is java consumer/producer so much faster than C++ Zoltan Juhasz <zoltan.juhasz@gmail.com> - 2012-07-23 06:57 -0700
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-23 18:42 +0200
Re: Why is java consumer/producer so much faster than C++ Howard Hinnant <howard.hinnant@gmail.com> - 2012-07-23 13:23 -0700
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-24 00:32 +0200
Re: Why is java consumer/producer so much faster than C++ Howard Hinnant <howard.hinnant@gmail.com> - 2012-07-23 18:11 -0700
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-24 11:21 +0200
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-24 21:45 +0200
Re: Why is java consumer/producer so much faster than C++ Luca Risolia <luca.risolia@studio.unibo.it> - 2012-07-24 23:24 +0200
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-25 00:48 +0200
Re: Why is java consumer/producer so much faster than C++ Luca Risolia <luca.risolia@studio.unibo.it> - 2012-07-25 01:21 +0200
Re: Why is java consumer/producer so much faster than C++ Howard Hinnant <howard.hinnant@gmail.com> - 2012-07-24 17:35 -0700
Re: Why is java consumer/producer so much faster than C++ Luca Risolia <luca.risolia@studio.unibo.it> - 2012-07-26 21:19 +0200
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-26 21:56 +0200
Re: Why is java consumer/producer so much faster than C++ Luca Risolia <luca.risolia@studio.unibo.it> - 2012-07-26 22:15 +0200
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-26 23:01 +0200
Re: Why is java consumer/producer so much faster than C++ Luca Risolia <luca.risolia@studio.unibo.it> - 2012-07-26 23:12 +0200
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-27 00:24 +0200
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-27 00:48 +0200
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-27 05:43 +0200
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-27 07:43 +0200
Re: Why is java consumer/producer so much faster than C++ Marc <marc.glisse@gmail.com> - 2012-07-26 21:10 +0000
Re: Why is java consumer/producer so much faster than C++ Donkey Hottie <donkey@fredriksson.dy.fi> - 2012-07-27 01:05 +0300
Re: Why is java consumer/producer so much faster than C++ Paavo Helde <myfirstname@osa.pri.ee> - 2012-07-26 17:12 -0500
Re: Why is java consumer/producer so much faster than C++ yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-07-24 12:51 +0000
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-24 17:47 +0200
Re: Why is java consumer/producer so much faster than C++ yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-07-25 08:09 +0000
Re: Why is java consumer/producer so much faster than C++ Howard Hinnant <howard.hinnant@gmail.com> - 2012-07-25 08:46 -0700
Re: Why is java consumer/producer so much faster than C++ Dombo <dombo@disposable.invalid> - 2012-07-23 22:57 +0200
Re: Why is java consumer/producer so much faster than C++ Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-23 21:54 -0700
Re: Why is java consumer/producer so much faster than C++ Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-07-24 12:50 +0000
Re: Why is java consumer/producer so much faster than C++ James Kanze <james.kanze@gmail.com> - 2012-07-29 03:27 -0700
Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-29 14:40 +0200
Re: Why is java consumer/producer so much faster than C++ Ricardo Nabinger Sanchez <rnsanchez@wait4.org> - 2012-08-14 22:48 +0000
csiph-web