Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To summarize the comments: There are two ways you can pass data. Either directly inside the void pointer, because "void pointer" is an integral type and thus can represent integers (but it doesn't necessarily have the same width as <code>int</code>), or indirectly by passing an actual address of the thing you care about.</p> <p><strong>Method 1 (pass the value):</strong></p> <pre><code>DWORD WINAPI Name(LPVOID lpParam) { intptr_t n = reinterpret_cast&lt;intptr_t&gt;(lpParam); // ... } int main() { intptr_t a = 10; thread1 = CreateThread(NULL, 0, Name, reinterpret_cast&lt;void *&gt;(a), 0, &amp;threadID); // ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } </code></pre> <p>For this method, we use the integral type <code>intptr_t</code>, which has the same width as <code>void *</code>. We use reinterpret-casts to store and retrieve arbitrary integral values.</p> <p><br></p> <p><strong>Method 2 (pass a pointer):</strong></p> <pre><code>DWORD WINAPI Name(LPVOID lpParam) { T * p = static_cast&lt;T *&gt;(lpParam); // ... use *p ... } int main() { T thing_I_care_about; thread1 = CreateThread(NULL, 0, Name, &amp;thing_I_care_about, 0, &amp;threadID); // ... ^^^^^^^^^^^^^^^^^^^ } </code></pre> <p>This is the more general method, but requires that <code>thing_I_care_about</code> remain alive, and it becomes a brittle shared state, so lifetime management and synchronisation become issues. Note that any object pointer is implicitly convertible to <code>void *</code>, so there's no need for the cast at the call site.</p> <p><br></p> <p>Finally, as others have commented, don't forget to join or detach your threads.</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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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