Note that there are some explanatory texts on larger screens.

plurals
  1. POpthread accessing it from a Class, variables are lost, when directly works fine
    primarykey
    data
    text
    <p>Well I'm trying to set a variable to use in a thread, it works fine if I call the pthread from the main function, but if I call it from a function or a function inside a class, the variable is lost and prints garbage instead, that's why I added the condition </p> <blockquote> <p>if(this->pickup &lt; 7)</p> </blockquote> <p>so I minimized the code so I could post it here because it has all the examples I'm saying.</p> <p>The output of this code below is: </p> <blockquote> <p>Access by Class:</p> <p>Hello, world! &lt;</p> <p>Access Directly:</p> <p>Hello, world!, N: 6&lt;</p> </blockquote> <p>I would like to have the same result as in Access Directly in Access by Class, I want it to output the ", N: 6" because after all it was defined. what am I missing here?</p> <p>I hope I was clear enough, thanks in advance.</p> <p>(By the way, I'm using the pthread library that is available for windows) so here is the code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;pthread.h&gt; #include &lt;iostream&gt; #include &lt;conio.h&gt; #include &lt;windows.h&gt; class C { public: int pickup; void *hello() { std::cout &lt;&lt; "\nHello, world!"; if(this-&gt;pickup &lt; 7) std::cout &lt;&lt; ", N: " &lt;&lt; this-&gt;pickup; std::cout &lt;&lt; "&lt;" &lt;&lt; std::endl; printf("HI"); return 0; } static void *hello_helper(void *context) { return ((C *)context)-&gt;hello(); } void StartThread(){ C c; c.pickup = 6; pthread_t t; pthread_create(&amp;t, NULL, &amp;C::hello_helper, &amp;c); } }; int main () { C c; std::cout &lt;&lt; "Access by Class: \n"; c.StartThread(); c.pickup = 6; Sleep(2000); std::cout &lt;&lt; "\nAccess Directly: \n"; pthread_t t; pthread_create(&amp;t, NULL, &amp;C::hello_helper, &amp;c); _getch(); return 0; } </code></pre>
    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.
 

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