---
name: "ck_sequence"
section: "3"
platform: "linux"
package: "libck-dev"
version: "0.7.2-6"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/ck_sequence"
---

# ck\_sequence(3)

sequence locks

## NAME

**ck\_sequence\_init**, **ck\_sequence\_read\_begin**, **ck\_sequence\_read\_retry**, **ck\_sequence\_write\_begin**, **ck\_sequence\_write\_end** – sequence locks

## LIBRARY

Concurrency Kit (libck, -lck)

## SYNOPSIS

**#include \<ck\_sequence.h\>**

ck\_sequence\_t seqlock = CK\_SEQUENCE\_INITIALIZER;

*void*\

**ck\_sequence\_init**(*ck\_sequence\_t \*sq*);

*unsigned* *int*\

**ck\_sequence\_read\_begin**(*const ck\_sequence\_t \*sq*);

*bool*\

**ck\_sequence\_read\_retry**(*const ck\_sequence\_t \*sq*, *unsigned int version*);

*void*\

**ck\_sequence\_write\_begin**(*ck\_sequence\_t \*sq*);

*void*\

**ck\_sequence\_write\_end**(*ck\_sequence\_t \*sq*);

## DESCRIPTION

It is recommended to use ck\_sequence when a small amount of data that cannot be accessed atomically has to be synchronized with readers in a fashion that does not block any writer. Readers are able to execute their read-side critical sections without any atomic operations. A ck\_sequence\_t must be initialized before use. It may be initialized using either a static initializer (CK\_SEQUENCE\_INITIALIZER) or using **ck\_sequence\_init**(). Before readers attempt to read data that may be concurrently modified they must first save the return value of **ck\_sequence\_read\_begin**(). While or after a reader has completed copying the data associated with a ck\_sequence\_t it must pass the earlier return value of **ck\_sequence\_read\_begin**() to **ck\_sequence\_read\_retry**(*.*, *If*) **ck\_sequence\_read\_retry**() returns true then the copy of data may be inconsistent and the read process must be retried. Writers must rely on their own synchronization primitives. Once a writer has entered its respective critical section, it must call **ck\_sequence\_write\_begin**() to signal intent to update the data protected by the ck\_sequence\_t. Before the writer leaves its critical section it must execute **ck\_sequence\_write\_end**() to indicate that the updates have left respective objects in a consistent state.

## EXAMPLE

```
#include <ck_sequence.h>
#include <stdlib.h>
static struct example {
	int a;
	int b;
	int c;
} global;
static ck_sequence_t seqlock = CK_SEQUENCE_INITIALIZER;
void
reader(void)
{
	struct example copy;
	unsigned int version;
	/*
	 * Attempt a read of the data structure. If the structure
	 * has been modified between ck_sequence_read_begin and
	 * ck_sequence_read_retry then attempt another read since
	 * the data may be in an inconsistent state.
	 */
	do {
		version = ck_sequence_read_begin(&seqlock);
		copy = global;
	} while (ck_sequence_read_retry(&seqlock, version));
	/*
	 * The previous may also be expressed using CK_SEQUENCE_READ.
	 * Generally recommend to only use ck_sequence_read_retry
	 * if you would like to detect a conflicting write at some
	 * higher granularity.
	 */
	CK_SEQUENCE_READ(&seqlock, &version) {
		copy = global;
	}
	return;
}
void
writer(void)
{
	for (;;) {
		ck_sequence_write_begin(&seqlock);
		global.a = rand();
		global.b = global.a + global.b;
		global.c = global.b + global.c;
		ck_sequence_write_end(&seqlock);
	}
	return;
}
```

## SEE ALSO

[ck\_brlock(3)](/ck_brlock/3.md), ck\_bytelock(3), [ck\_rwlock(3)](/ck_rwlock/3.md)

Additional information available at http://concurrencykit.org/
