| Last change
 on this file since d73e667 was             9c65169, checked in by Peter A. Buhr <pabuhr@…>, 10 months ago | 
        
          | 
reduce yields in test but still get deterministic results
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            969 bytes | 
      
      
| Line |  | 
|---|
| 1 | #include <thread.hfa> | 
|---|
| 2 | #include <fstream.hfa> | 
|---|
| 3 |  | 
|---|
| 4 | // Test priority order of waitfor clauses. To get deterministic output for repeatable testing, preemption is disabled | 
|---|
| 5 | // and the threads defer to each other at certain points. | 
|---|
| 6 |  | 
|---|
| 7 | Duration default_preemption() { return 0; }                             // disable preemption | 
|---|
| 8 |  | 
|---|
| 9 | enum { Times = 50, Half = Times / 2 }; | 
|---|
| 10 |  | 
|---|
| 11 | thread T {}; | 
|---|
| 12 | void f( T & mutex ) {} | 
|---|
| 13 | void g( T & mutex ) {} | 
|---|
| 14 | void main( T & t ) { | 
|---|
| 15 | sout | nlOff; | 
|---|
| 16 | for ( Times / Half ) { | 
|---|
| 17 | for ( Half ) { | 
|---|
| 18 | waitfor( f : t ) sout | "f"; | 
|---|
| 19 | or waitfor( g :t ) sout | "g";                          // service f before g | 
|---|
| 20 | } // for | 
|---|
| 21 | for ( Half ) { | 
|---|
| 22 | waitfor( g : t ) sout | "g";                            // service g before f | 
|---|
| 23 | or waitfor( f : t )     sout | "f"; | 
|---|
| 24 | } // for | 
|---|
| 25 | sout | "\n"; | 
|---|
| 26 | } // for | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | T t;                                                                                                    // shared | 
|---|
| 30 |  | 
|---|
| 31 | thread F{}; | 
|---|
| 32 | void main( F & ) { | 
|---|
| 33 | for ( Half ) f( t ); | 
|---|
| 34 | for ( Half ) f( t ); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | thread G{}; | 
|---|
| 38 | void main( G & ) { | 
|---|
| 39 | for ( Half ) g( t ); | 
|---|
| 40 | for ( Half ) g( t ); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | int main() { | 
|---|
| 44 | F f; | 
|---|
| 45 | yield();                                                                                        // let f get ahead | 
|---|
| 46 | G g; | 
|---|
| 47 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.