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 }
Matrix3f m
Matrix expected
Definition: testMatrix.cpp:971
Array< int, Dynamic, 1 > v
TEST_SUBMODULE(thread, m)
Definition: test_thread.cpp:33
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1882


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46