---
title: "Poll groups"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/kernel/poll/
last_updated: 2026-11-13
sdk_version: 4.44
---
# Poll groups

Source: https://developer.repebble.com/pebbleos/docs/samples/kernel/poll/

This sample is an event loop: one thread that waits on a queue of button events and a queue of accelerometer samples at once, each with its own message type, and handles whichever has something.

The wait returns the queue and leaves the message in it, so the loop receives it with the type that queue carries.

Run:

`pbl samples samples/kernel/poll`

Boards:

every board

## Sources

src/main.c

```
#include \<inttypes.h\>#include \<pbl/kernel/delay.h\>#include \<pbl/kernel/msgq.h\>#include \<pbl/kernel/poll.h\>#include \<pbl/kernel/thread.h\>#include \<pbl/logging/logging.h\>struct button\_event {uint8\_t button;bool pressed;};struct accel\_sample {int16\_t x;int16\_t y;int16\_t z;};/\* One queue per source, each with its own message type and depth. \*/static PBL\_MSGQ\_DEFINE(s\_buttons, sizeof(struct button\_event), 4);static PBL\_MSGQ\_DEFINE(s\_accel, sizeof(struct accel\_sample), 8);static PBL\_POLL\_GROUP\_DEFINE(s\_events);PBL\_THREAD\_STACK\_DEFINE(s\_source\_stack, 1024);static struct pbl\_thread s\_source;static void prv\_source(void \*arg) {const struct button\_event press = {.button = 1, .pressed = true};const struct button\_event release = {.button = 1, .pressed = false};const struct accel\_sample sample = {.x = 10, .y = -20, .z = 1000};pbl\_msgq\_put(&s\_buttons, &press, PBL\_FOREVER);pbl\_sleep\_ms(10);pbl\_msgq\_put(&s\_accel, &sample, PBL\_FOREVER);pbl\_sleep\_ms(10);pbl\_msgq\_put(&s\_buttons, &release, PBL\_FOREVER);}static void prv\_handle(struct pbl\_msgq \*q) {/\* The wait leaves the message in its queue. \*/if (q == &s\_buttons) {struct button\_event event;if (pbl\_msgq\_get(q, &event, PBL\_NO\_WAIT) == 0) {PBL\_LOG\_INFO("loop: button %u %s", event.button, event.pressed ? "pressed" : "released");}} else if (q == &s\_accel) {struct accel\_sample sample;if (pbl\_msgq\_get(q, &sample, PBL\_NO\_WAIT) == 0) {PBL\_LOG\_INFO("loop: accel %d %d %d", sample.x, sample.y, sample.z);}}}int main(void) {const struct pbl\_thread\_attr attr = {.name = "source",.entry = prv\_source,.prio = CONFIG\_KERNEL\_MAIN\_THREAD\_PRIO - 1,.privileged = true,.stack = s\_source\_stack,.stack\_size = sizeof(s\_source\_stack),};/\* Queues join before the first wait, and for good. \*/pbl\_poll\_group\_add(&s\_events, &s\_buttons);pbl\_poll\_group\_add(&s\_events, &s\_accel);pbl\_thread\_create(&s\_source, &attr);for (;;) {struct pbl\_msgq \*q = pbl\_poll\_group\_wait(&s\_events, PBL\_MSEC(100));if (q == NULL) {PBL\_LOG\_INFO("loop: nothing for 100 ms");break;}prv\_handle(q);}PBL\_LOG\_INFO("Sample done");return 0;}
```

## Output

```
loop: button 1 pressed
loop: accel 10 -20 1000
loop: button 1 released
loop: nothing for 100 ms
Sample done
```

Generated from PebbleOS [f4926c952](https://github.com/coredevices/pebbleos/commit/f4926c952bf9b82bce9e3cdb44645a59531c0ee9). This page is maintained in the pebbleos repository: [docs/samples/kernel/poll.md](https://github.com/coredevices/pebbleos/blob/f4926c952bf9b82bce9e3cdb44645a59531c0ee9/docs/samples/kernel/poll.md).
