首页 \ 问答 \ 如何强制fseek()移动光标(How to force fseek() to move the cursor)

如何强制fseek()移动光标(How to force fseek() to move the cursor)

我有一个程序,它创建多个线程,每个线程尝试在不同位置(偏移)的文件中写入100个字节。 第一个线程从0开始写入100个字节,从100开始写入第二个100个字节,从300开始写入第三个100个字节,依此类推如果线程按此顺序执行,则可以使用,并且我不需要fseek。 但是对于实时并发,如果我将第一个线程置于“sleep(2)”2秒钟,等待所有其他线程完成,并使用fseek将文件光标移动到文件的开头,这不会发生。 我使用互斥锁来处理并发。 代码示例:

    offset=0;//for the first thread
    char data[100];
    int length; // how many chars are currently in data
    FILE * f;

    pthread_mutex_lock(&mutexFileWrite);
    f = fopen(fileName, "a");
    fseek(f,offset, SEEK_SET);  
    fwrite(data,sizeof(char),length,f);
    fclose(f);
    pthread_mutex_unlock(&mutexFileWrite);

I have a program which creates multiple threads and each one of them tries to write 100 bytes in a file at a different location(offset). The first thread writes 100 bytes starting from 0, the second 100 bytes starting from 100, the third 100 bytes starting from 300 and so on If the threads are executed in this order everithing is ok and I do not need fseek. But for real time concurrency if I put the first thread to "sleep(2)" for 2 seconds, wait until all other threads are done, and use fseek to move the file cursor to the begining of the file this doesn't happen. I used mutexes to handle concurrency. Code sample:

    offset=0;//for the first thread
    char data[100];
    int length; // how many chars are currently in data
    FILE * f;

    pthread_mutex_lock(&mutexFileWrite);
    f = fopen(fileName, "a");
    fseek(f,offset, SEEK_SET);  
    fwrite(data,sizeof(char),length,f);
    fclose(f);
    pthread_mutex_unlock(&mutexFileWrite);

原文:https://stackoverflow.com/questions/14081265
更新时间:2021-12-18 19:12

最满意答案

我建议不要重命名库。 创建软链接是更可取的。 我不确定apt-get如何处理由用户而不是apt-get本身删除或重命名的文件。

libboost_regex.so很可能是libboost_regex.so.1.46.1的软链接


I would advise against renaming the library. Creating a soft link is preferable. I am not sure how apt-get deals with files that are deleted or renamed by a user instead of by apt-get itself.

libboost_regex.so is most likely a soft link to libboost_regex.so.1.46.1

相关问答

更多
  • 原因是boost windows头文件添加了一些pragma,可以自动查找.lib文件自动填充它的名字。 这是导致cmake查找库“libboost_program_options-vc140-mt-1_64.lib”的全名的原因。 我通过添加: add_definitions(-DBOOST_ALL_NO_LIB) # disabling of auto filling boost libs names to versioned The reason for it was that boost win ...
  • 编辑:删除初始语句,因为对帖子的编辑改变了情况。 基于http://www.boost.org/doc/libs/1_53_0/more/getting_started/unix-variants.html#library-naming (由Igor R.提供): libboost_thread-vc110-mt-1_52.lib是一个静态库(不需要DLL)boost_thread-vc110-mt-1_52.lib是DLL的导入库 你只需要使用其中之一。 asking myquestion myself. ...
  • 我怀疑它只是没有读取你的user-config.jam ...当构建引用并搜索user-config.jam时,它在%HOME%位置中执行此操作。 不在Boost源的根目录中,也不在当前的目录中。 但是你可以使用一个特定的user-config.jam文件: bjam --user-config=user-config.jam ... 哪些只会在当前目录中查找。 或者你也可以指定文件的完整路径。 您应该阅读mpi.jam工具文件( mpi.jam )中的注释,因为它解释了在哪些情况下它会自动设置mpi工具。 ...
  • 您可以使用/usr/local/opt/boost/lib或/usr/local/lib 。 You can use /usr/local/opt/boost/lib or /usr/local/lib.
  • 有一些有用的标志可以帮助FindBoost查找库。 在调用find_package(Boost)之前尝试设置这些变量(打开或关闭 - 这取决于您的boost安装find_package(Boost) set(Boost_LIB_PREFIX "lib" CACHE STRING "") set(Boost_USE_MULTITHREADED ON CACHE BOOL "") # '-mt' flag set(Boost_USE_STATIC_LI ...
  • 为了使系统在程序的运行时找到.dll文件,您需要将.dll所在的目录添加到PATH环境变量中。 例如从控制台(运行程序之前) C:\Users\XYZ> set PATH="%PATH%;c:\directory_of_dll" In order to make the system to find a .dll file at runtime of a program, you need to add the directory where the .dll is located to the PATH ...
  • -gd用于libs的调试版本。 使用-gd-mt构建调试配置,使用-mt进行发布配置。 -gd is for debug version of libs. Use -gd-mt for building in debug configuration and -mt for release configurations.
  • 文档和我的实际安装之间略有不同。 如果文档中的文件中包含“boost_1_42_0”,则安装程序会将路径设置为“boost_1_42”。 有了这个固定,它的工作原理。 There's a slight difference between the documentation and my actual installation. Where the documentation has "boost_1_42_0" in the path, the installer made my path "boost_ ...
  • 我建议不要重命名库。 创建软链接是更可取的。 我不确定apt-get如何处理由用户而不是apt-get本身删除或重命名的文件。 libboost_regex.so很可能是libboost_regex.so.1.46.1的软链接 I would advise against renaming the library. Creating a soft link is preferable. I am not sure how apt-get deals with files that are deleted o ...
  • 我发现了原因。 事实证明我正在使用支持C ++ 11的标准库。 为了解决这个问题,我使用brew重建了boost如下 brew -v install --with-icu --build-from-source --with-c++11 boost I found out why. Turns out that I'm using the standard library with C++11 support. To fix the issue, I rebuilt boost using bre ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)