-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <nanobind/nanobind.h> | ||
#include <atomic> | ||
|
||
namespace nb = nanobind; | ||
|
||
struct Counter { | ||
std::atomic<size_t> counter { 0 }; | ||
void inc() { counter++; } | ||
}; | ||
|
||
NB_MODULE(test_thread_ext, m) { | ||
nb::class_<Counter>(m, "Counter") | ||
.def(nb::init<>()) | ||
.def_prop_ro("counter", [](Counter &c) { return (size_t) c.counter; }) | ||
.def("inc", &Counter::inc); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Temporarily turn off immortalization | ||
try: | ||
from test.support import suppress_immortalization | ||
except ImportError: | ||
from contextlib import nullcontext as suppress_immortalization | ||
|
||
import test_thread_ext as t | ||
|
||
import threading | ||
import gc | ||
|
||
def parallelize(func, n_threads): | ||
with suppress_immortalization(True): # Avoid reference leak errors | ||
barrier = threading.Barrier(n_threads) | ||
|
||
def wrapper(): | ||
barrier.wait() | ||
return func() | ||
|
||
workers = [] | ||
for _ in range(n_threads): | ||
t = threading.Thread(target=wrapper) | ||
t.start() | ||
workers.append(t) | ||
|
||
for worker in workers: | ||
worker.join() | ||
|
||
def test01_object_creation(): | ||
from test_thread_ext import Counter | ||
|
||
def f(): | ||
n = 1000000 | ||
r = [None]*n | ||
for i in range(n): | ||
r[i] = Counter() | ||
del r | ||
|
||
parallelize(f, n_threads=8) | ||
gc.collect() |