source: tests/concurrent/channels/ping_pong.cfa @ 7c2c37a

ADTast-experimental
Last change on this file since 7c2c37a was 9319a23, checked in by caparson <caparson@…>, 19 months ago

added channel tests that use new termination feature

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <locks.hfa>
2#include <fstream.hfa>
3#include <stdio.h>
4#include <channel.hfa>
5#include <thread.hfa>
6#include <time.hfa>
7#include <string.h>
8
9typedef channel( int ) Channel;
10
11Channel * ping;
12Channel * pong;
13
14bool done = false;
15size_t total_operations = 0;
16
17thread Pong {};
18void main(Pong & this) {
19    try {
20        for ( ;; ) {
21            if ( done ) break;
22            insert( *ping, 0 );
23            remove( *pong );
24        }
25    } catch ( channel_closed * e ) {}
26}
27
28thread Ping {};
29void main(Ping & this) {
30    try {
31        for ( ;; ) {
32            if ( done ) break;
33            remove( *ping );
34            insert( *pong, 1 );
35            total_operations++;
36        }
37    } catch ( channel_closed * e ) {}
38}
39
40
41int main( int argc, char * argv[] ) {
42    sout | "start";
43    processor proc[1];
44
45    Channel pingChan{ 1 };
46    Channel pongChan{ 1 };
47
48    ping = &pingChan;
49    pong = &pongChan;
50   
51    {
52        Ping pi;
53        Pong po;
54        sleep(10`s);
55        done = true;
56        close( *pong );
57        close( *ping );
58    }
59
60    // sout | total_operations;
61   
62    sout | "done";
63    return 0;
64}
Note: See TracBrowser for help on using the repository browser.