| 1 | #include <iostream>
|
|---|
| 2 | #include <fstream>
|
|---|
| 3 | #include <unistd.h>
|
|---|
| 4 |
|
|---|
| 5 | #include <boost/interprocess/sync/file_lock.hpp>
|
|---|
| 6 |
|
|---|
| 7 | using namespace std;
|
|---|
| 8 | using namespace boost::interprocess;
|
|---|
| 9 |
|
|---|
| 10 | int main(int argc, char *argv[]) {
|
|---|
| 11 | file_lock flock("lock");
|
|---|
| 12 |
|
|---|
| 13 | if (flock.try_lock()) cout << "Acquired lock" << endl;
|
|---|
| 14 | else {
|
|---|
| 15 | cout << "Could not acquire lock" << endl;
|
|---|
| 16 | return 1;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | ifstream file("lock");
|
|---|
| 20 |
|
|---|
| 21 | // If you comment out the next line the lock stays active during the sleep.
|
|---|
| 22 | file.close(); // also closes lock
|
|---|
| 23 |
|
|---|
| 24 | sleep(30);
|
|---|
| 25 |
|
|---|
| 26 | return 0;
|
|---|
| 27 | }
|
|---|