1887

Passing std::promise object to a function via direct function call

<h3>Question</h3>

I am learning the std::promise and std::future in C++. I wrote one simple program to calculate the multiplication of two numbers.

void product(std::promise<int> intPromise, int a, int b) { intPromise.set_value(a * b); } int main() { int a = 20; int b = 10; std::promise<int> prodPromise; std::future<int> prodResult = prodPromise.get_future(); // std::thread t{product, std::move(prodPromise), a, b}; product(std::move(prodPromise), a, b); std::cout << "20*10= " << prodResult.get() << std::endl; // t.join(); }

In the above code if I invoke the product function using threads it's working fine. But if I invoke the function using direct function call I am getting the following error:

terminate called after throwing an instance of 'std::system_error' what(): Unknown error -1 Aborted (core dumped)

I added some logs to check the problem. I am getting the error while setting the value (set_value) in the function product. Is there anything I missed in the code?


<h3>Answer1:</h3>

When you compile this code, even if don't use std::thread explicitly, you still have to add -pthread command line option, because internally std::promise and std::future depend on the pthread library.

Without -pthread on my machine I get:

terminate called after throwing an instance of 'std::system_error' what(): Unknown error -1

With -pthread:

20*10 = 200 <hr /><blockquote>

My doubt is if std::promise using std::thread then it should throw some compilation or linkage error right?

</blockquote>

Very good question. See my answer here.

来源:https://stackoverflow.com/questions/62307634/passing-stdpromise-object-to-a-function-via-direct-function-call

Recommend

  • Rails 5 API POST Create params empty
  • MethodNotAllowedException Laravel Password Grant API
  • How to monitor video and https traffic using bro network security monitor
  • How to return dynamic object from operator function?
  • Rotating only the MapView's content
  • How to change Language in android WebView
  • Reference after posting data from web worker
  • Facebook like button redirect? [closed]
  • Storing the Cursor for App Engine Pagination
  • Laravel 5 - Cache remember doesn't work
  • Complex multiple if statements
  • C# ICMPv6 checksum calculation
  • How to reduce a DAG by replacing each longest non-branching path by an edge connecting the start and
  • what do lines starting with double-slash '//' mean in a .npmrc?
  • How can i use JQuery fadeTo() in IE 7?
  • How to close a WebView with double-click?
  • How to fetch asset modification history in hyperledger fabric
  • How to put an object in the air?
  • C++11: Why rvalue reference parameter implicitly converted to lvalue
  • Adding native code to an existing Worklight hybrid app
  • How to include WYSIWYG text editor in an HTML using google script
  • Circular Left Rotation Algorithm in C#
  • opencv deskewing a contour
  • What are advantages/disadvantages of using Selenium for Java vs .NET applications?
  • How to use array in autohotkey?
  • Disable account chooser FirebaseUI React
  • SQL Server 2012 not showing unicode character in results
  • Regex not working in java 1.5
  • Java .policy file - how to prevent java.util.Date() from being accessible
  • how to read to huge file into buffer
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • Geokit in Ruby on Rails, problem with acts_as_mappable
  • Terminal run dalvikvm with am.jar
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Google App Engine Datastore: Dealing with eventual consistency
  • How to handle div that is created dynamically in a table
  • ssh remote server login script
  • Make checkout phone field optional for specific countries in WooCommerce
  • How to call different template for different category archive page in woocommerce
  • ReferenceError: TextEncoder is not defined