# Multithreading

Operators and functions for working with threads (**thread** type) are described here.

* [Lock()](#lock)
* [resume( thread th )](#resume-thread-th)
* [SetThreadData(obj o)](#setthreaddata-obj-o)
* [sleep( int duration )](#sleep-int-duration)
* [suspend( thread th )](#suspend-thread-th)
* [terminate( thread th )](#terminate-thread-th)
* [ThreadData() obj](#threaddata-obj)
* [Unlock()](#unlock)
* [wait( thread th )](#wait-thread-th)
* [WaitAll()](#waitall)
* [WaitDone()](#waitdone)
* [WaitGroup( int count )](#waitgroup-int-count)

## Operators

| Operator            | Result | Description          |
| ------------------- | ------ | -------------------- |
| thread **=** thread |        | Assignment operator. |

## Functions

### Lock()

The *Lock* function blocks access to the global resource (mutex). If it is already occupied by another thread, then the current thread is waiting for it to be released. The mutex must be freed using the *Unlock* function.

### resume(thread th)

The *resume* function continues the work of the thread that was stopped by *suspend* function.

### SetThreadData(obj o)

The *SetThreadData* function assigns a variable of type *obj* to the current thread. The value of the variable can be retrieved with the *ThreadData* function.

### sleep(int duration)

The *sleep* function pauses the current thread for at least the *duration*, in milliseconds.

### suspend(thread th)

The *suspend* function suspends the *th* thread. Use the *resume* function to continue the thread.

### terminate(thread th)

The *terminate* function terminates the thread. If the thread has already completed, the function does nothing.

### ThreadData() obj

The *ThreadData* function returns the object that was assigned to the current thread. A variable of type *obj* is assigned to the thread by the function *SetThreadData*.

### Unlock()

The *Unlock* function releases access to a global resource (mutex).

### wait(thread th)

The *wait* function waits for the *th* thread to finish. If the thread has already finished, the function does nothing.

### WaitAll()

The *WaitAll* function waits when the *WaitGroup* counter becomes zero.

```go
run {
  int count = 3
  WaitGroup(count)
  for i in 1..count {
    go {
      // ...
      WaitDone()
    }
  }
  WaitAll()
}
```

### WaitDone()

The *WaitDone* function decreases the counter *WaitGroup* by one.

### WaitGroup(int count)

The *WaitGroup* function creates a *WaitGroup* counter for the threads that should end with a calling the *WaitDone* function. *count* - the initial value of the counter.
