I have a main.c with a global variable called int countboards
. 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 rmmain(...)
which is in the rm.c (RM=Ressource Manager). In rm.c I read countboards
, in the progserver.c in the pthread I write to this variable (both are made accessible by extern int countboards
).
So the problem is, when I write to countboards
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?
main.c:
int countboards;
int main(int argc, char** argv) {
countboards = 0;
pthread_t thread;
pthread_create(&thread, NULL, startProgramserver, NULL);
rmmain();
return 0;
}
rm.c:
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 >0, but it isn't
...
}
}
programserver.c:
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
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…