test_thread.cpp
Go to the documentation of this file.
1 /*
2  tests/test_thread.cpp -- call pybind11 bound methods in threads
3 
4  Copyright (c) 2021 Laramie Leavitt (Google LLC) <lar@google.com>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #include <pybind11/cast.h>
11 #include <pybind11/pybind11.h>
12 
13 #include "pybind11_tests.h"
14 
15 #include <chrono>
16 #include <thread>
17 
18 namespace py = pybind11;
19 
20 namespace {
21 
22 struct IntStruct {
23  explicit IntStruct(int v) : value(v) {};
24  ~IntStruct() { value = -value; }
25  IntStruct(const IntStruct &) = default;
26  IntStruct &operator=(const IntStruct &) = default;
27 
28  int value;
29 };
30 
31 } // namespace
32 
33 TEST_SUBMODULE(thread, m) {
34 
35  py::class_<IntStruct>(m, "IntStruct").def(py::init([](const int i) { return IntStruct(i); }));
36 
37  // implicitly_convertible uses loader_life_support when an implicit
38  // conversion is required in order to lifetime extend the reference.
39  //
40  // This test should be run with ASAN for better effectiveness.
41  py::implicitly_convertible<int, IntStruct>();
42 
43  m.def("test", [](int expected, const IntStruct &in) {
44  {
45  py::gil_scoped_release release;
46  std::this_thread::sleep_for(std::chrono::milliseconds(5));
47  }
48 
49  if (in.value != expected) {
50  throw std::runtime_error("Value changed!!");
51  }
52  });
53 
54  m.def(
55  "test_no_gil",
56  [](int expected, const IntStruct &in) {
57  std::this_thread::sleep_for(std::chrono::milliseconds(5));
58  if (in.value != expected) {
59  throw std::runtime_error("Value changed!!");
60  }
61  },
62  py::call_guard<py::gil_scoped_release>());
63 
64  // NOTE: std::string_view also uses loader_life_support to ensure that
65  // the string contents remain alive, but that's a C++ 17 feature.
66 }
cast.h
TEST_SUBMODULE
TEST_SUBMODULE(thread, m)
Definition: test_thread.cpp:33
conf.release
release
Definition: gtsam/3rdparty/GeographicLib/python/doc/conf.py:69
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
init
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:2006
pybind11.h
pybind11_tests.h
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
pybind11
Definition: wrap/pybind11/pybind11/__init__.py:1
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:09