访问不同 c 文件中 pthread 中的全局变量
我有一个 main.c,其中有一个名为 int countboards
的全局变量。在 main() 中,我启动一个 pthread,它监听一个 TCP 连接并通过 (progserver.c) 运行它。意味着该线程永远不会返回。在 main() 中,我输入 rm.c 中的函数 rmmain(...)
(RM=资源管理器)。在 rm.c 中,我读取了 countboards
,在 pthread 的 progserver.c 中,我写入了该变量(两者都可以通过 extern int countboards
访问)。
所以问题是,当我在 pthread 中写入 countboards
时,我想在 rm.c 中写入该变量后访问该变量,它仍然具有旧值(在本例中为 0)例如10)。为什么?
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
...
}
}
程序服务器.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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您会在每个线程中看到一个缓存的副本。我建议将其声明为 volatile int countboards ,但这确实不是一个好方法。
全局变量有点邪恶。通过将指针传递给每个线程并与互斥体同步,您会得到更好的服务。
编辑:由于我昨晚很匆忙,所以要对此进行扩展...
http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/
作为 KasigiYabu 在下面的评论中提到,创建一个“上下文”结构,其中包含您想要在线程之间共享的所有信息并将其作为最后一个参数传递给 pthread_create 是一种合理的方法,并且在大多数情况下我也是这样做的。
You're seeing a cached copy in each thread. I would suggest declaring it
volatile int countboards
except that's really not a good way to go about things.Globals are kinda evil. You'd be better served by passing a pointer to each thread and synchronizing with a mutex.
Edit: To expand on this since I was in a hurry last night ...
http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/
As KasigiYabu mentions in the comments below, creating a "context" structure that contains all the information you want to share between the threads and passing that in to
pthread_create
as the last arg is a sound approach and is what I do as well in most cases.