Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These are just reformulations of <a href="https://stackoverflow.com/a/2251472/315052">Steve Jessop's C solution</a> into C++. These toy examples (note the lack of error checking) use templates to make it obvious how to change the code to use some type other than <code>double</code>. For example, the <code>double</code> could be replaced with a <code>class</code> type if more than one value needed to be returned after the work is done. In practice, the base class and template would likely be removed and the <code>work()</code> method of <code>MyWorker</code> would be called directly by the invoker rather than via a virtual method.</p> <p>First, using <code>pthread</code>:</p> <pre><code>#include &lt;iostream&gt; #include &lt;pthread.h&gt; class WorkerBase { protected: virtual ~WorkerBase () {} public: virtual void * work () = 0; }; template &lt;typename T&gt; struct Worker : public WorkerBase { T result; }; extern "C" void *invoke_worker (void *arg) { return static_cast&lt;WorkerBase *&gt;(arg)-&gt;work(); } struct MyWorker : public Worker&lt;double&gt; { void * work () { result = 4.2; return 0; } }; int main () { pthread_t t; MyWorker w; pthread_create(&amp;t, 0, invoke_worker, &amp;w); pthread_join(t, 0); std::cout &lt;&lt; "result: " &lt;&lt; w.result &lt;&lt; std::endl; return 0; } </code></pre> <p>Second, using C++11 <code>std::thread</code>:</p> <pre><code>#include &lt;iostream&gt; #include &lt;thread&gt; class WorkerBase { protected: virtual ~WorkerBase () {} public: virtual void work () = 0; static void invoke (WorkerBase *w) { w-&gt;work(); } }; template &lt;typename T&gt; struct Worker : public WorkerBase { T result; }; class MyWorker : public Worker&lt;double&gt; { void work () { result = 4.2; } }; int main () { MyWorker w; std::thread t(MyWorker::invoke, &amp;w); t.join(); std::cout &lt;&lt; "result: " &lt;&lt; w.result &lt;&lt; std::endl; return 0; } </code></pre> <p>You had another question in your post that I had missed:</p> <blockquote> <p>And if I have a static array of 10 positions and 10 pthreads modifiying different positions each time, will I have trouble?</p> </blockquote> <p>Whether or not this gives you trouble will likely depend on the array element type and your hardware architecture. In practice, I have not observed this being a problem on x86 architectures for array elements that are aligned on machine word boundaries.</p>
    singulars
    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