| 1 | #include <iostream>
|
|---|
| 2 | #include <chrono>
|
|---|
| 3 | #include <thread>
|
|---|
| 4 | #include <vector>
|
|---|
| 5 | #include <boost/container/pmr/synchronized_pool_resource.hpp>
|
|---|
| 6 | #include <boost/thread/sync_queue.hpp>
|
|---|
| 7 | #include <boost/thread.hpp>
|
|---|
| 8 |
|
|---|
| 9 | #include <sched.h>
|
|---|
| 10 |
|
|---|
| 11 | boost::container::pmr::synchronized_pool_resource pool;
|
|---|
| 12 | boost::concurrent::sync_queue<void*> queue;
|
|---|
| 13 |
|
|---|
| 14 | void cleaner()
|
|---|
| 15 | {
|
|---|
| 16 | void * ptr;
|
|---|
| 17 | while ( queue.wait_pull(ptr) == boost::concurrent::queue_op_status::success )
|
|---|
| 18 | {
|
|---|
| 19 | pool.deallocate(ptr, 512, 4);
|
|---|
| 20 | }
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | void worker()
|
|---|
| 24 | {
|
|---|
| 25 | struct sched_param param = { .sched_priority = 1 };
|
|---|
| 26 | const int res = sched_setscheduler(0, SCHED_FIFO, ¶m);
|
|---|
| 27 |
|
|---|
| 28 | if ( res < 0 )
|
|---|
| 29 | {
|
|---|
| 30 | perror("sched_setscheduler");
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | for ( int i = 0; i < 1000; ++i )
|
|---|
| 34 | {
|
|---|
| 35 | auto now = std::chrono::steady_clock::now();
|
|---|
| 36 | auto p = pool.allocate(512, 4);
|
|---|
| 37 | auto end = std::chrono::steady_clock::now();
|
|---|
| 38 | if ( std::chrono::duration_cast<std::chrono::milliseconds>(end - now).count() > 1000 )
|
|---|
| 39 | {
|
|---|
| 40 | std::cout << "failed " << i << std::endl;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | queue.push(p);
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | int main(int argc, char **argv)
|
|---|
| 48 | {
|
|---|
| 49 | unsigned nthreads = boost::thread::hardware_concurrency();
|
|---|
| 50 | std::cout << "Running on " << nthreads << " threads " << std::endl;
|
|---|
| 51 |
|
|---|
| 52 | std::vector< boost::thread > thr;
|
|---|
| 53 |
|
|---|
| 54 | for ( int i = 0; i < nthreads; ++i )
|
|---|
| 55 | {
|
|---|
| 56 | thr.emplace_back( worker );
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | boost::thread c(cleaner);
|
|---|
| 60 |
|
|---|
| 61 | for ( auto & t : thr )
|
|---|
| 62 | {
|
|---|
| 63 | t.join();
|
|---|
| 64 | }
|
|---|
| 65 | queue.close();
|
|---|
| 66 | c.join();
|
|---|
| 67 |
|
|---|
| 68 | return 0;
|
|---|
| 69 | }
|
|---|