如何避免使用nftw在使用全局变量 [英] How avoid using global variable when using nftw

查看:84
本文介绍了如何避免使用nftw在使用全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用nftw在C遍历目录结构。

不过,鉴于我想做的事情,我没有看到周围使用全局变量的方式。

使用(n)的FTW都涉及做这样打印出的文件名的课本上的例子。我想,相反,采取的路径名和文件校验并将那些在一个数据结构中。但我没有看到一个很好的办法做到这一点,因为什么可以传递给nftw的限制。

我使用该解决方案包括一个全局变量。然后通过nftw调用的函数可以访问该变量,并添加所需的数据。

有任何合理的方式做到这一点不使用全局变量?

<一个href=\"http://stackoverflow.com/questions/10251390/c-can-a-function-return-a-global-variable/10251799#comment13196161_10251799\">Here's在计算器中,有人建议我张贴这是一个后续的交流,previous职位。


解决方案

使用FTW可真的,真的。内部,它可以节省您使用的函数指针,如果另一个线程然后做别的东西将覆盖函数指针。


  

恐怖的场景:

 线程1:数十亿个文件
线程2:删除一些文件
线程1:---哎呀,现在删除了数十亿
              文件,而不是指望他们。


在做空。你最好使用fts_open。

如果您仍然想使用nftw那么我的建议是把全局类型的命名空间,其标记为thread_local。你应该能够调整此您的需求。

  / *在某些cpp文件* /
命名空间{
   thread_local为size_t gTotalBytes {0}; //线程局部使得这个线程安全
INT的getSize(为const char *路径,常量结构统计* statPtr,INT currentFlag,结构FTW * internalFtwUsage){
    gTotalBytes + = statPtr-&GT; st_size;
    返回0; // ntfw继续
 }
} //命名空间
为size_t RecursiveFolderDiskUsed(常量标准::字符串&安培; startPath){
   const int的旗帜= FTW_DEPTH | FTW_MOUNT | FTW_PHYS;
   const int的maxFileDescriptorsToUse = 1024; // 管他呢
   const int的结果= nftw(startPath.c_str()的getSize,maxFileDescriptorsToUse,旗);  //日志或东西,如果结果== -1
  返回gTotalBytes;
}

I want to use nftw to traverse a directory structure in C.

However, given what I want to do, I don't see a way around using a global variable.

The textbook examples of using (n)ftw all involve doing something like printing out a filename. I want, instead, to take the pathname and file checksum and place those in a data structure. But I don't see a good way to do that, given the limits on what can be passed to nftw.

The solution I'm using involves a global variable. The function called by nftw can then access that variable and add the required data.

Is there any reasonable way to do this without using a global variable?

Here's the exchange in previous post on stackoverflow in which someone suggested I post this as a follow-up.

解决方案

Using ftw can be really, really bad. Internally it will save the the function pointer that you use, if another thread then does something else it will overwrite the function pointer.

Horror scenario:

thread 1:  count billions of files
thread 2:  delete some files
thread 1:  ---oops, it is now deleting billions of 
              files instead of counting them.

In short. You are better off using fts_open.

If you still want to use nftw then my suggestion is to put the "global" type in a namespace and mark it as "thread_local". You should be able to adjust this to your needs.

/* in some cpp file */
namespace {
   thread_local size_t gTotalBytes{0};  // thread local makes this thread safe
int GetSize(const char* path, const struct stat* statPtr, int currentFlag, struct FTW* internalFtwUsage) {
    gTotalBytes+=  statPtr->st_size;
    return 0;  //ntfw continues
 }
} // namespace


size_t RecursiveFolderDiskUsed(const std::string& startPath) {
   const int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;
   const int maxFileDescriptorsToUse = 1024; // or whatever
   const int result = nftw(startPath.c_str(), GetSize, maxFileDescriptorsToUse , flags);

  // log or something if result== -1
  return gTotalBytes;
}

这篇关于如何避免使用nftw在使用全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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