What's the most effecient way to pass a copy of std::shared_ptr into a C function/API/thread that takes only a const void* parameter?

huangapple go评论99阅读模式
英文:

What's the most effecient way to pass a copy of std::shared_ptr into a C function/API/thread that takes only a const void* parameter?

问题

如果我有一个在不同线程中调用回调的 C 函数(或 API),并以 const void* 的形式接受一个老式 C 参数,那么将 std::shared_ptr 的副本传递到该回调的最有效方式是什么?

到目前为止,我一直在使用以下方法:

(我将使用一个线程来模拟我所问的。)

  1. struct My1
  2. {
  3. My1()
  4. {
  5. std::cout << "My1 constructor" << std::endl;
  6. }
  7. ~My1()
  8. {
  9. std::cout << "My1 destructor" << std::endl;
  10. }
  11. };
  12. void* thread1(void* pParam)
  13. {
  14. std::shared_ptr<My1>* pp1 = (std::shared_ptr<My1>*)pParam;
  15. std::cout << "thread1 ref count: " << pp1->use_count() << std::endl;
  16. delete pp1;
  17. return 0;
  18. }
  19. int main(int argc, const char * argv[])
  20. {
  21. if(1)
  22. {
  23. std::shared_ptr<My1> p1 = std::make_shared<My1>();
  24. std::cout << "ref count: " << p1.use_count() << std::endl;
  25. std::shared_ptr<My1>* pp1 = new std::shared_ptr<My1>(p1);
  26. pthread_t thr1;
  27. if(pthread_create(&thr1, nullptr, thread1, (void*)pp1) != 0)
  28. {
  29. //Failed to start thread
  30. delete pp1;
  31. }
  32. std::cout << "ref count: " << p1.use_count() << std::endl;
  33. }
  34. int i;
  35. std::cin >> i;
  36. return 0;
  37. }
英文:

Say, if I have a C function (or an API) that invokes a callback in a different thread and accepts an old-school C parameter in a form of const void*. What's the most efficient way to pass a copy of the std::shared_ptr into that callback?

So far, I've been using the following method:

(I'll use a thread to simulate what I'm asking.)

  1. struct My1
  2. {
  3. My1()
  4. {
  5. std::cout &lt;&lt; &quot;My1 constructor&quot; &lt;&lt; std::endl;
  6. }
  7. ~My1()
  8. {
  9. std::cout &lt;&lt; &quot;My1 destructor&quot; &lt;&lt; std::endl;
  10. }
  11. };
  12. void* thread1(void* pParam)
  13. {
  14. std::shared_ptr&lt;My1&gt;* pp1 = (std::shared_ptr&lt;My1&gt;*)pParam;
  15. std::cout &lt;&lt; &quot;thread1 ref count: &quot; &lt;&lt; pp1-&gt;use_count() &lt;&lt; std::endl;
  16. delete pp1;
  17. return 0;
  18. }
  19. int main(int argc, const char * argv[])
  20. {
  21. if(1)
  22. {
  23. std::shared_ptr&lt;My1&gt; p1 = std::make_shared&lt;My1&gt;();
  24. std::cout &lt;&lt; &quot;ref count: &quot; &lt;&lt; p1.use_count() &lt;&lt; std::endl;
  25. std::shared_ptr&lt;My1&gt;* pp1 = new std::shared_ptr&lt;My1&gt;(p1);
  26. pthread_t thr1;
  27. if(pthread_create(&amp;thr1, nullptr, thread1, (void*)pp1) != 0)
  28. {
  29. //Failed to start thread
  30. delete pp1;
  31. }
  32. std::cout &lt;&lt; &quot;ref count: &quot; &lt;&lt; p1.use_count() &lt;&lt; std::endl;
  33. }
  34. int i;
  35. std::cin &gt;&gt; i;
  36. return 0;
  37. }

答案1

得分: 1

由于您只需要代码部分的翻译,这里是代码的翻译:

自您需要一份拷贝,只需在thread1中使用拷贝构造函数创建一个实例。另外,请不要使用new来创建shared_ptr,这没有太多意义。

示例:

  1. #include <pthread.h>
  2. #include <iostream>
  3. #include <memory>
  4. struct My1 {
  5. My1() { std::cout << "My1 constructor\n"; }
  6. ~My1() { std::cout << "My1 destructor\n"; }
  7. };
  8. void* thread1(void* pParam) {
  9. // 拷贝构造:
  10. std::shared_ptr<My1> pp1 = *static_cast<std::shared_ptr<My1>*>(pParam);
  11. std::cout << "thread1 ref count: " << pp1.use_count() << '\n';
  12. return nullptr;
  13. }
  14. int main() {
  15. std::shared_ptr<My1> p1 = std::make_shared<My1>();
  16. std::cout << "ref count: " << p1.use_count() << '\n';
  17. pthread_t thr1;
  18. if (pthread_create(&thr1, nullptr, thread1, &p1) != 0) {
  19. // 线程启动失败
  20. } else {
  21. pthread_join(thr1, nullptr); // 等待线程结束
  22. }
  23. std::cout << "ref count: " << p1.use_count() << '\n';
  24. }

输出:

  1. My1 constructor
  2. ref count: 1
  3. thread1 ref count: 2
  4. ref count: 1
  5. My1 destructor
英文:

Since you want a copy, just create an instance in thread1 using the copy constructor. Also, don't create the shared_ptrs using new. It doesn't make much sense.

Example:

  1. #include &lt;pthread.h&gt;
  2. #include &lt;iostream&gt;
  3. #include &lt;memory&gt;
  4. struct My1 {
  5. My1() { std::cout &lt;&lt; &quot;My1 constructor\n&quot;; }
  6. ~My1() { std::cout &lt;&lt; &quot;My1 destructor\n&quot;; }
  7. };
  8. void* thread1(void* pParam) {
  9. // copy construct:
  10. std::shared_ptr&lt;My1&gt; pp1 = *static_cast&lt;std::shared_ptr&lt;My1&gt;*&gt;(pParam);
  11. std::cout &lt;&lt; &quot;thread1 ref count: &quot; &lt;&lt; pp1.use_count() &lt;&lt; &#39;\n&#39;;
  12. return nullptr;
  13. }
  14. int main() {
  15. std::shared_ptr&lt;My1&gt; p1 = std::make_shared&lt;My1&gt;();
  16. std::cout &lt;&lt; &quot;ref count: &quot; &lt;&lt; p1.use_count() &lt;&lt; &#39;\n&#39;;
  17. pthread_t thr1;
  18. if (pthread_create(&amp;thr1, nullptr, thread1, &amp;p1) != 0) {
  19. // Failed to start thread
  20. } else {
  21. pthread_join(thr1, nullptr); // wait for the thread to finish
  22. }
  23. std::cout &lt;&lt; &quot;ref count: &quot; &lt;&lt; p1.use_count() &lt;&lt; &#39;\n&#39;;
  24. }

Output:

  1. My1 constructor
  2. ref count: 1
  3. thread1 ref count: 2
  4. ref count: 1
  5. My1 destructor

答案2

得分: 0

I am going to assume that the lifetime of the callback thread may exceed the lifetime of the original shared_ptr used when calling into the API. If this is not the case and the original shared_ptr is guaranteed to live longer than the callback thread, you can dispense with the smart pointer and just pass in a raw pointer and not worry. I presume this is not the case.

This is not aesthetically pleasing code, but I think it is a reasonable (only?) option for the situation you describe of calling into an API that invokes a callback you provide.

英文:

I am going to assume that the lifetime of the callback thread may exceed the lifetime of the original shared_ptr used when calling into the API. If this is not the case and the original shared_ptr is guaranteed to live longer than the callback thread, you can dispense with the smart pointer and just pass in a raw pointer and not worry. I presume this is not the case.

This is not aesthetically pleasing code, but I think it is a reasonable (only?) option for the situation you describe of calling into an API that invokes a callback you provide.

答案3

得分: 0

不要动态分配共享指针;在此处使用自动存储期;否则使用智能指针的整个意义基本上丧失。

此外,由于您在主线程上执行 delete pp1;,不能保证在后台线程访问时共享指针对象仍然存在。

在这里,您需要一种方式来传达后台线程已经拥有了对象的事实,并以一种方式来建立上下文对象与后台线程拥有共享指针之间的发生前关系。

std::latch

英文:

Do not dynamically allocate shared pointers; work with automatic storage duration here; otherwise the whole point of using smart pointers is basically lost.

Furthermore since you do delete pp1; on the main thread, it's in no way guaranteed that the shared pointer object is still alive when you access it in the background thread.

What you need here is a way of communicating the fact that the background thread has taken ownership of the object and doing so in a way that establishes a happens before relationship between the context object and the background thread taking ownership of the shared pointer.

std::latch happens to be something that can accompilish this. You can work around the const-ness of the context by using mutable (. Note: pthread does not receive the context as pointer to const).

Note: I'm using std::thread here for simplicity, but since it's used with a function pointer and a void pointer, you should easily be able to adjust the logic for use with pthread or other C multithreading logic.

  1. struct ThreadContext
  2. {
  3. mutable std::latch m_latch{1};
  4. mutable std::shared_ptr&lt;std::string&gt; m_ptr = std::make_shared&lt;std::string&gt;(&quot;Hello World&quot;);
  5. };
  6. void BackgroundThreadLogic(void const* context)
  7. {
  8. std::shared_ptr&lt;std::string&gt; ptr;
  9. {
  10. auto const tk = static_cast&lt;ThreadContext const*&gt;(context);
  11. ptr = std::move(tk-&gt;m_ptr);
  12. tk-&gt;m_latch.count_down(); // after this the context may become a dangling pointer any time, but we&#39;ve taken ownership of the important stuff already
  13. }
  14. std::cout &lt;&lt; &quot;Message received on background thread: &quot; &lt;&lt; *ptr &lt;&lt; &#39;\n&#39;;
  15. }
  16. int main() {
  17. std::thread t;
  18. {
  19. ThreadContext context;
  20. t = std::thread(&amp;BackgroundThreadLogic, static_cast&lt;void const*&gt;(&amp;context));
  21. context.m_latch.wait();
  22. // now we can delete the context
  23. }
  24. t.join(); // still need to join the thread in the end
  25. }

huangapple
  • 本文由 发表于 2023年5月22日 02:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定