source:
doc/papers/concurrency/examples/PingPong.cc
@
0583b47b
Last change on this file since 0583b47b was 466fa01, checked in by , 5 years ago | |
---|---|
|
|
File size: 1.2 KB |
Line | |
---|---|
1 | #include <cstdio> |
2 | |
3 | struct Obj { |
4 | Obj() { printf( "Obj constructor\n" ); } |
5 | ~Obj() { printf( "Obj destructor\n" ); } |
6 | int i = 4; |
7 | }; |
8 | |
9 | struct PingPong { |
10 | const char * name; |
11 | const int N; |
12 | PingPong * partner = nullptr; |
13 | int i = 0; |
14 | void * next = nullptr; |
15 | |
16 | PingPong( const char * name, int N ) : name(name), N(N) {} |
17 | |
18 | void operator()() { |
19 | if ( __builtin_expect(next != 0, 1) ) goto *next; |
20 | next = &&cycle; |
21 | for ( ; i < N; i += 1 ) { |
22 | Obj obj; |
23 | printf( "X %s %d\n", name, obj.i ); |
24 | obj.i = 7; |
25 | #ifdef PRINT |
26 | printf( "%s %d\n", name, i ); |
27 | #endif // PRINT |
28 | asm( "mov %0,%%rdi" : "=m" (partner) ); |
29 | asm( "mov %rdi,%rax" ); |
30 | #ifndef OPT |
31 | #ifdef PRINT |
32 | asm( "add $40, %rsp" ); |
33 | #endif // PRINT |
34 | asm( "popq %rbp" ); |
35 | #endif // ! OPT |
36 | |
37 | #ifdef OPT |
38 | #ifdef PRINT |
39 | asm( "popq %rbx" ); |
40 | #endif // PRINT |
41 | #endif // OPT |
42 | asm( "jmp _ZN8PingPongclEv" ); |
43 | cycle: ; |
44 | printf( "Y %s %d\n", name, obj.i ); |
45 | } // for |
46 | } |
47 | }; |
48 | int main() { |
49 | enum { N = |
50 | #ifdef PRINT |
51 | 5 |
52 | #else |
53 | 1000000000 |
54 | #endif // PRINT |
55 | }; |
56 | PingPong ping = { "ping", N }, pong = { "pong", N }; |
57 | ping.partner = &pong; pong.partner = &ping; |
58 | ping(); |
59 | } |
60 | |
61 | // Local Variables: // |
62 | // tab-width: 4 // |
63 | // compile-command: "g++-8 -g -DPRINT PingPong.cc" // |
64 | // End: // |
Note: See TracBrowser
for help on using the repository browser.