memgraph/mpsc.cpp

39 lines
712 B
C++
Raw Normal View History

2015-10-07 03:02:47 +08:00
#include <iostream>
#include <vector>
#include <thread>
#include <atomic>
#include "debug/log.hpp"
2015-10-07 03:02:47 +08:00
static constexpr int N = 1000;
using q_t = lockfree::MpscQueue<std::string>;
void produce()
2015-10-07 03:02:47 +08:00
{
std::hash<std::thread::id> hasher;
for(int i = 0; i < N; ++i)
{
auto str = "Hello number " + std::to_string(i) + " from thread " +
2015-10-07 03:02:47 +08:00
std::to_string(hasher(std::this_thread::get_id()));
LOG_DEBUG(str);
2015-10-07 03:02:47 +08:00
}
}
int main(void)
{
constexpr int THREADS = 256;
std::vector<std::thread> threads;
for(int i = 0; i < THREADS; ++i)
threads.push_back(std::thread([]() { produce(); }));
2015-10-07 03:02:47 +08:00
for(auto& thread : threads)
thread.join();
return 0;
}