如何强制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
最满意答案
我建议不要重命名库。 创建软链接是更可取的。 我不确定
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 byapt-get
itself.
libboost_regex.so
is most likely a soft link tolibboost_regex.so.1.46.1
相关问答
更多-
Cmake通过全名寻找提升lib,而不是明确地说短的(Cmake looking for boost lib by full name instead of explicitly said short one)[2023-11-25]
原因是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 ... -
链接错误:模糊的libboost * .lib与boost * .lib(linking error: ambiguous libboost*.lib vs boost*.lib)[2023-11-27]
编辑:删除初始语句,因为对帖子的编辑改变了情况。 基于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. ... -
试图构建Boost MPI,但不创建lib文件。(trying to build Boost MPI, but the lib files are not created. What's going on?)[2023-10-02]
我怀疑它只是没有读取你的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.
-
CLION和Boost 1.60.0(CLion and Boost 1.60.0)[2023-09-29]
有一些有用的标志可以帮助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 ... -
Boost找不到lib(Boost can not find lib)[2023-06-12]
为了使系统在程序的运行时找到.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 ... -
Boost -gd lib文件(Boost -gd lib file)[2023-07-05]
-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错误(Linking with Boost error)[2021-12-31]
文档和我的实际安装之间略有不同。 如果文档中的文件中包含“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 ...