]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/performance/thread/skynet_pthread.cpp
18 #include "buffered_channel.hpp"
20 using channel_type
= buffered_channel
< std::uint64_t >;
21 using clock_type
= std::chrono::steady_clock
;
22 using duration_type
= clock_type::duration
;
23 using time_point_type
= clock_type::time_point
;
33 void * skynet( void * vargs
) {
34 thread_args
* args
= static_cast< thread_args
* >( vargs
);
35 if ( 1 == args
->size
) {
36 args
->c
.push( args
->num
);
38 channel_type rc
{ 16 };
39 for ( std::size_t i
= 0; i
< args
->div
; ++i
) {
40 auto sub_num
= args
->num
+ i
* args
->size
/ args
->div
;
41 auto sub_size
= args
->size
/ args
->div
;
44 if ( 0 != ::pthread_attr_init( & tattr
) ) {
45 std::runtime_error("pthread_attr_init() failed");
47 if ( 0 != ::pthread_attr_setstacksize( & tattr
, size
) ) {
48 std::runtime_error("pthread_attr_setstacksize() failed");
50 thread_args
* targs
= new thread_args
{ rc
, sub_num
, sub_size
, args
->div
};
52 if ( 0 != ::pthread_create( & tid
, & tattr
, & skynet
, targs
) ) {
53 std::runtime_error("pthread_create() failed");
55 if ( 0 != ::pthread_detach( tid
) ) {
56 std::runtime_error("pthread_detach() failed");
59 std::uint64_t sum
{ 0 };
60 for ( std::size_t i
= 0; i
< args
->div
; ++i
) {
61 sum
+= rc
.value_pop();
71 std::size_t size
{ 10000 };
72 std::size_t div
{ 10 };
73 std::uint64_t result
{ 0 };
74 duration_type duration
{ duration_type::zero() };
76 thread_args
* targs
= new thread_args
{ rc
, 0, size
, div
};
77 time_point_type start
{ clock_type::now() };
79 result
= rc
.value_pop();
80 duration
= clock_type::now() - start
;
81 std::cout
<< "Result: " << result
<< " in " << duration
.count() / 1000000 << " ms" << std::endl
;
82 std::cout
<< "done." << std::endl
;
84 } catch ( std::exception
const& e
) {
85 std::cerr
<< "exception: " << e
.what() << std::endl
;
87 std::cerr
<< "unhandled exception" << std::endl
;