Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suggest that you read up on the documentation for the functions you use. From your comment in James Hopkin's answer, it seems like you don't know what boost::bind does, but simply copy-pasted the code.</p> <p>boost::bind takes a function (call it f), and optionally a number of parameters, and returns a function which, when called, calls f with the specified parameters.</p> <p>That is, <code>boost::bind(threadedAPI1, 0)()</code> (creating a function which takes no arguments and calls threadedAPI1() with the argument 0, and then calling that) is equivalent to <code>threadedAPI1(0)</code>.</p> <p>Since your threadedAPI functions don't actually take any parameters, you can't pass any arguments to them. That is just fundamental C++. You can't call <code>threadedAPI1(0)</code>, but only <code>threadedAPI1()</code>, and yet when you call the function, you try (via boost::bind) to pass the integer 0 as an argument.</p> <p>So the simple answer to your question is to simply define threadedAPI1 as follows:</p> <pre><code>int threadedAPI1(int i); </code></pre> <p>However, one way to avoid the boost::bind calls is to call a functor instead of a free function when launching the thread. Declare a class something like this:</p> <pre><code>struct threadedAPI { threadedAPI(int i) : i(i) {} // A constructor taking the arguments you wish to pass to the thread, and saves them in the class instance. void operator()() { // The () operator is the function that is actually called when the thread starts, and because it is just a regular class member function, it can see the 'i' variable initialized by the constructor cout &lt;&lt; "Thread" &lt;&lt; i &lt;&lt; endl; // No need to create 4 identical functions. We can just reuse this one, and pass a different `i` each time we call it. } private: int i; }; </code></pre> <p>Finally, depending on what you need, plain threads may be better suited than a threadpool. In general, a thread pool only runs a limited number of threads, so it may queue up some tasks until one of its threads finish executing. It is mainly intended for cases where you have many short-lived tasks.</p> <p>If you have a fixed number of longer-duration tasks, creating a dedicated thread for each may be the way to go.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload