访问全局变量在不同的C-文件的pthreads [英] Accessing global variables in pthreads in different c-files

查看:156
本文介绍了访问全局变量在不同的C-文件的pthreads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫做全局变量的main.c INT countboards 。在main()我通过(progserver.c)开始的pthread,侦听一个TCP-连接并运行。手段,这个线程将永远不会返回。在main()我进入功能 rmmain(...)这是在rm.c(RM =的ressource管理器)。在rm.c我读 countboards ,在我写这个变量的pthread的progserver.c(均由做访问的extern INT countboards )。

所以,问题是,当我写 countboards 在pthread的,我想访问这个变量它在rm.c被写入后,它仍然有旧的值(在这种情况下,0而不是例如10)。为什么呢?

main.c中:

  INT countboards;INT主(INT ARGC,字符** argv的){
  countboards = 0;
  线程的pthread_t;
  在pthread_create(安培;螺纹,NULL startProgramserver,NULL);  rmmain();  返回0;
}

rm.c:

 的extern INT countboards;INT rmmain(vhbuser * vhbuserlist,INT countvhbuser,
       userio * useriolist,诠释countios,诠释usertorm,诠释rmtosslserver,诠释sslservertorm){
  而(1){
    INT N;
    N =读取(usertorm,BUF,BUFC); //阻塞,直到命令来自用户
    ...
    板* ​​B = findAFreeBoard(boardlist,countboards,usagelist); //这里countboards应大于0,但它不
    ...
  }
}

programserver.c:

 的extern INT countboards;
无效* startProgramserver(无效*){
  ...
  袜子= tcp_listen();
  ...
  http_serve(SSL,S,sslpipes);
}静态INT http_serve(SSL * SSL,int类型,无效* sslpipes){
  ...
  countboards = countboards + countboardscommands;
  ...
  //此处countboards有新的价值
}


解决方案

您所看到的每个线程缓存副本。我会建议宣布它挥发性INT countboards 除了真的没有去了解事情的好办法。

全局还挺邪恶。你会用一个指针传递给每个线程和同步与互斥得到更好的服务。

编辑::要扩大这一点,因为我很着急,昨晚...

<一个href=\"http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/\" rel=\"nofollow\">http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/

由于 KasigiYabu 提及在下面的意见,创建包含所有你想要共享信息的背景结构线程之间和传球,在以在pthread_create 作为最后的arg是一个健全的办法,就是我在大多数情况下做的一样好。

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
}

解决方案

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.

这篇关于访问全局变量在不同的C-文件的pthreads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆