# Independent Updates

## The Basic Pattern

extern int x; // ... ANNOTATE_SITE_BEGIN(site1); for (i = 0; i != n; ++i) { ANNOTATE_ITERATION_TASK(task1); x = x + a[i]; } ANNOTATE_SITE_END(site1); printf("%d\n", x);

- Task 0 reads
.x - Task 1 reads
.x - Task 0 adds
to the value it read and stores the result back ina[0].x - Task 1 adds
to the value it read and stores the result back ina[1], overwriting the value stored by task 0.x

`can be performed in any order. All you need to do is to make sure that no task can write tox`

`between the read fromx`

`and the write tox`

`in any other task; the uses ofx`

`in the tasks are otherwise independent.x`

## Reductions

*Reductions*

## Transactions

void insert_node_in_list(T *new_node, T *insert_after) { new_node->next = insert_after->next; new_node->prev = insert_after->next->prev; insert_after->next->prev = new_node; insert_after->next = new_node; }

*transaction*

## Guard Variables

bool initialized = false; void do_something() { if (!initialized) { do_the_initialization(); initialized = true; } do_the_real_work(); }

`is called from multiple tasks, then the sharing problem is:do_something()`

- Task 0 reads
, which is false, and enters the body of theinitializedstatement.if - Task 1 reads
, which is false, and enters the body of theinitializedstatement, soifis called twice.do_the_initialization()

## Independent Writes

bool found = false; ANNOTATE_SITE_BEGIN(site1); for (i = 0; i != n; ++i) { ANNOTATE_ITERATION_TASK(task1); if (a[i] == b) found = true; } if (found) printf("found\n"); ANNOTATE_SITE_BEGIN(site1);

`at all, it will write the value true.found`

found = found || (a[i] == b);