Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing global variables in pthreads in different c-files
    primarykey
    data
    text
    <p>I have a main.c with a global variable called <code>int countboards</code>. In the main() I start a pthread, that listens to ONE TCP-Connection and runs that through (progserver.c). Means, this thread will never return. In the main() I enter the function <code>rmmain(...)</code> which is in the rm.c (RM=Ressource Manager). In rm.c I read <code>countboards</code>, in the progserver.c in the pthread I write to this variable (both are made accessible by <code>extern int countboards</code>).</p> <p>So the problem is, when I write to <code>countboards</code> in the pthread and I want to access this variable after it's been written to in the rm.c, it still has the old value (in this case 0 instead of for example 10). Why?</p> <p>main.c:</p> <pre><code>int countboards; int main(int argc, char** argv) { countboards = 0; pthread_t thread; pthread_create(&amp;thread, NULL, startProgramserver, NULL); rmmain(); return 0; } </code></pre> <p>rm.c:</p> <pre><code>extern int countboards; int rmmain(vhbuser* vhbuserlist, int countvhbuser, userio* useriolist, int countios, int usertorm, int rmtosslserver, int sslservertorm) { while(1) { int n; n=read(usertorm,buf,bufc); // blocks until command comes from the user ... board* b = findAFreeBoard(boardlist, countboards, usagelist); // here countboards should be &gt;0, but it isn't ... } } </code></pre> <p>programserver.c:</p> <pre><code>extern int countboards; void* startProgramserver(void*) { ... sock = tcp_listen(); ... http_serve(ssl,s, sslpipes); } static int http_serve(SSL *ssl, int s, void* sslpipes) { ... countboards = countboards + countboardscommands; ... // here countboards has the new value } </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.
 

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