Skip to content

Commit 8148340

Browse files
authored
Add context destructor (#58)
1 parent d75578e commit 8148340

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ public:
321321
my_context() {
322322
initialize_stack_memory(m_stack);
323323
}
324+
~my_context() {
325+
// ‼️ The most derived context must call cancel in its destructor
326+
cancel();
327+
// If memory was allocated, deallocate it here...
328+
}
324329

325330
private:
326331
void do_schedule(async::blocked_by p_state,
@@ -330,6 +335,21 @@ private:
330335
};
331336
```
332337

338+
#### Initialization
339+
340+
In order to create a usable custom context, the stack memory must be
341+
initialized with a call to `initialize_stack_memory(span)` with a span to the
342+
memory for the stack. There is no requirements of where this memory comes from
343+
except that it be a valid source. Such sources can be array thats member of
344+
this object, dynamically allocated memory that the context has sole ownership
345+
of, or it can point to statically allocated memory that it has sole control and
346+
ownership over.
347+
348+
#### Destruction
349+
350+
The custom context must call `cancel()` before deallocating the stack memory.
351+
Once cancel completes, the stack memory may be deallocated.
352+
333353
### Using basic_context with sync_wait
334354

335355
```cpp
@@ -340,6 +360,10 @@ public:
340360
simple_context() {
341361
initialize_stack_memory(m_stack);
342362
}
363+
364+
~simple_context() {
365+
cancel();
366+
}
343367
};
344368

345369
simple_context ctx;

0 commit comments

Comments
 (0)