> For the complete documentation index, see [llms.txt](https://pcj.icm.edu.pl/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pcj.icm.edu.pl/manual/shared-variables.md).

# Shareable variables

The general rule is that variables are local to the tasks and cannot be accessed from another task. PCJ offers possibility to mark some variables as \_`Shareable`\_using Java annotation mechanism. The Shareable variables are accessible across tasks, e.g. one task can get access to the shareable variable instance stored in another task. Shareable variables have to be defined as part of enum variable and have to be registered using `@RegisterStorage` annotation.

```java
@RegisterStorage(ExampleGetPut.Vars.class)    // Register storage 
public class ExampleGetPut implements StartPoint {

    @Storage(ExampleGetPut.class)               // Define storage
    enum Vars {
        a, array
    }

    public double a = PCJ.threadCount();
    public double array[] = new double[10];
 
[...]
}
```

The elements of storage can be single variables, arrays as well as more complicated objects.

### Access to a shareable variables

The PCJ library provides methods to access shared variables, eg. to get value stored in the memory of another task (`get()`) or to modify variable located in the memory of another task (`put()`).

Both methods: `get()` and `put()` perform one-sided communication. This means, that access to the memory of another task is performed only by task which executes get or put methods. The task which memory is contacted do not need to execute these methods.

The example code presents how to assign value of the variable `a` at task 3 to the variable `c` at task 0.

```java
if (PCJ.myId()==0) c = (double) PCJ.get(3, Vars.a);
```

The same for assigning value of the variable `array[2]`at task 3 to the variable `c` at task 0.

```java
if (PCJ.myId()==0) c = (double) PCJ.get(3, Vars.array, 3);
```

Next example presents how to assign value 3.0 to the variable `a` available at the task 5. This operation is performed by the task 0.

```java
if (PCJ.myId()==0) PCJ.put(3.0, 5, Shared.a);
```

Similar, we send value of 4.0 to the `array[]` stored at the task 2. The `array[1]` element is updated.

```java
if (PCJ.myId()==0) PCJ.put(4.0, 2, Shared.array, 1);
```

The communication is performed in synchronous way. The `asyncPut()` and `asyncGet()` can be used for asynchronous communication which means that user has no guarantee that value has been changed or transferred from remote task. The use of asynchronous communication may cause some problems, especially for non experienced users. PCJ provides additional methods to solve this problem.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://pcj.icm.edu.pl/manual/shared-variables.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
