Product SiteDocumentation Site

Name

tc_parallel_for, tc_parallel, tc_with, tc_parallel_end — libtcr convenience macros for parallel computation

Synopsis

#include <tc/threaded_cr.h>

tc_parallel_for(VAR, INIT_ST, COND_ST, INC_ST) { BODY }

tc_parallel { BODY1 } tc_with { BODY2 } tc_parallel_end;

Description

The tc_parallel_for(,,,) macro is similar to the C language's for(;;) statement. It potentially executes iterations of the body in parallel.
It has a few limitations that must be kept in mind
  • Each execution thread of the statement's body has in independent instance of the counter variable. Modifying it in the body has no effect on the other iterations.
  • The statement's body has to be enclosed in braces { } I.e. the following example does not compile:
    tc_parallel_for(i, i = 0, i < 1000, i++)
    	printf("%d\n", i);
    	    
    Valid is:
    tc_parallel_for(i, i = 0, i < 1000, i++) {
    	printf("%d\n", i);
    }
    	    
  • It is implemented as a multiple statement macro therefore it can not be used as a sole statement. I.e. the following example does not compile:
    if (...)
    	tc_parallel_for(i, i = 0, i < 1000, i++) {
    	    
    Valid is:
    if (...) {
    	tc_parallel_for(i, i = 0, i < 1000, i++) {
    		...
    	}
    }
    	    
The tc_parallel { body1 } tc_with { body2 } tc_parallel_end; macros execute the body1 in parallel with body2.
This macro can be used as sole statement, as well the two bodies might be sole statements. Example:
tc_parallel
	printf("p1\n");
tc_with
	printf("p2\n");
tc_parallel_end;
      

Author

Philipp Reisner