0

I've been exploring C++'s concurrency features, specifically around futures and promises. I came across two different ways to obtain a std::shared_future, and I'm trying to understand if there's any significant difference between them.

Method 1: Directly from std::promise

std::promise<int> prms;
std::shared_future<int> fut = prms.get_future();

Method 2: Converting a std::future to std::shared_future

std::promise<int> prms;
std::future<int> fut = prms.get_future();
std::shared_future<int> sfut = fut.share();

Functionally, do these two methods result in any difference in behavior, performance, or other aspects? Is one method preferred over the other in certain scenarios?

Sami
  • 513
  • 4
  • 11
  • 1
    I don't see any difference. – HolyBlackCat Aug 27 '23 at 16:45
  • 1
    My advice would be to focus on other concurrency features first. While I was kind of excited about the *idea* of promises and futures when they were new, I've yet to find much real use for them (but use `std::thread`/`std::jthread` all the time). – Jerry Coffin Aug 27 '23 at 17:20
  • @JerryCoffin Thanks for your advice. Would you please tell me what are the most practical concurrency features that I need to focus on them first? Thank you for your time. – Sami Aug 27 '23 at 17:31
  • 2
    @Sami: As I already mentioned, I find thread and jthread quite useful. Condition variables and atomic variables are also quite useful. I'd start with really knowing an understanding a thread-safe queue. Although it doesn't cure every problem, one common pattern is a thread that pulls some data from a queue, and processes it (and as often as not, afterwards pushes the result to another queue). – Jerry Coffin Aug 27 '23 at 21:32
  • @JerryCoffin Thank you so much. Appreciate it. I implemented a circular queue as an actor/model in a multithreaded application and it really is a cool technique! – Sami Aug 28 '23 at 00:00

0 Answers0