Need clarification on file_lock documentation
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Documentation
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- cpp
- Domain
- documentation, operating-systems
Research direction
Start with the boost::interprocess::file_lock documentation and reproduce the behavior using the C++ example included in the issue. Determine whether the documented thread and process guidance is incorrect or merely unclear; done means the documentation or implementation question has a resolved, verified explanation.
Written by the indexing model from the issue text.
Description
The documentation of boost::interprocess::file_lock has some cautions on synchronization limitations:
- It's unspecified if a file_lock synchronizes two threads from the same process.
- It's unspecified if a process can use two file_lock objects pointing to the same file.
But then gives the guidance:
- For each file, use a single file_lock object per process.
- Use the same thread to lock and unlock a file.
I then created the following example:
https://wandbox.org/permlink/dO2rRDCHVWBE3pGrhttps://wandbox.org/permlink/3CNfkA3RcMkwbd2v
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/filesystem.hpp>
#include <mutex>
#include <thread>
#include <chrono>
int main()
{
std::ofstream("f.txt");
boost::interprocess::file_lock lock("f.txt");
std::mutex mtx;
const size_t max = 100;
size_t t1Count = 0;
std::thread
t1
(
[&lock,&t1Count,&mtx,max]()
{
while(t1Count < max)
{
//std::lock_guard<std::mutex> mtxLock(mtx);
boost::interprocess::scoped_lock<boost::interprocess::file_lock> guard(lock);
std::cout << "Number 1" << std::endl;
t1Count++;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
);
size_t t2Count = 0;
std::thread
t2
(
[&lock,&t2Count,&mtx,max]()
{
while(t2Count < max)
{
//std::lock_guard<std::mutex> mtxLock(mtx);
boost::interprocess::scoped_lock<boost::interprocess::file_lock> guard(lock);
std::cout << "Number 2" << std::endl;
t2Count++;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
);
t1.join();
t2.join();
return 0;
}
It only works properly if the std::mutex is used in addition to the file_lock. That was originally my thought when I read "It's unspecified if a file_lock synchronizes two threads from the same process.", but it seemed like I was following the guidance "Use the same thread to lock and unlock a file.". If I haven't messed anything up, I would probably update the documentation to say something like:
"If utilitizing the file_lock from multiple threads within the same process, synchronize access to the file_lock with an intraprocess synchronization mechanism such as a mutex."
Otherwise, is it a bug?
Thanks,
Rob
- Dominant language
- C++
- Stars
- 185
- Forks
- 131
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/interprocess
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
boostorg/interprocess#196 · 1 reaction ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
boostorg/interprocess#279 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
boostorg/interprocess#274 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 28/100
boostorg/interprocess#268 · 4 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
boostorg/interprocess#262 · 2 comments ·
All issues in boostorg/interprocess
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
objectionary/eo-graphs#74 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 95/100
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 88/100
QuantStack/git2cpp#187 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100