| FBB::SharedCondition(3bobcat) | Shared Memory Cond. Var. | FBB::SharedCondition(3bobcat) |
FBB::SharedCondition - Shared Memory Condition Variable
#include <bobcat/sharedcondition>
Linking option: -lpthread, -lbobcat
Condition variables are used to synchronize threads based on the values of data. Condition variables allow threads to wait until a certain condition has occurred, after which the threads continue their actions. Thus waiting threads don’t continuously have to poll the state of a variable (requiring the threads to gain access to the variable before they can inspect its value). Using condition variables waiting threads simply wait until they are notified.
SharedCondition objects can be used in combination with shared memory. SharedCondition objects interface to objects (called Condition objects in this man-page) which are defined in shared memory and contain a SharedMutex and a shared condition object. These Condition objects may be accessed by threads running in different processes. These different processes might run a single main thread, or they themselves can be multi-threaded.
Condition variables are used in situations like these:
While the first thread is waiting, it is suspended. It may be resumed when it receives a notification from another thread, but also for spurious reasons. Therefore the first thread must verify that the condition has been met after resuming its actions.
As condition variables are always used in combination with a mutex, SharedMutex encapsulates the mutex-handling. The software using SharedCondition objects doesn’t have to handle the mutex itself.
SharedCondition objects are used to synchronize actions by different processes, using shared memory as their vehicle of synchronization/communication. The actual condition variable that is used by a SharedCondition object is defined in shared memory. SharedCondition objects themselves are small objects, containing the necessary information to access the actual shared memory condition variable.
FBB
All constructors, members, operators and manipulators, mentioned in this
man-page, are defined in the namespace FBB.
Default, copy, and move constructors as well as the copy and move assignment operators are available.
Returning from SharedCondition member functions the offset of the SharedMemory object in which the condition variable has been defined has not changed. Internally, the current offset is saved; the requested function is performed; and the original offset is restored. Consequently, SharedCondition member functions can be used disregarding the SharedMemory’s current offset.
As the sharedCondition.lock ... sharedCondition.unlock sequence itself may be executed at different flow of control sections, the unlock member cannot be called from within notify.
sharedCondition.lock(); // lock the mutex
... // operate on the condition’s variables
if (conditionWasMet) // ready to notify
sharedCondition.notify();
sharedCondition.unlock(); // release the lock
sharedCondition.lock(); // lock the mutex
while (conditionWasNotYetMet) // waiting required
sharedCondition.wait();
... // do something: we have the lock
sharedCondition.unlock(); // release the lock
When returning from wait_for the current thread has obtained the shared condition’s lock, but maybe due to a timeout: this can be verified by inspecting wait_for’s return value, and an appropriate action can be selected.
sharedCondition.lock(); // lock the mutex
while (conditionWasNotYetMet) // waiting required
{
while (sharedCondition.wait_for(someTime)
== std::cv_status::timeout)
handle_timeout
do_something
}
sharedCondition.unlock(); // release the lock
std::chrono::system_clock::now() + std::chrono::seconds(5)
#include <iostream>
#include <bobcat/sharedcondition>
#include <bobcat/sharedmemory>
using namespace std;
using namespace FBB;
int main(int argc, char **argv)
try
{
if (argc == 1)
{
cout <<
"Argument:\n"
" c: create a shared memory segment + SharedCondition "
", display ID\n"
" k <id>: kill shared memory segment <id>\n"
" m <id>: show a message every 5 secs, otherwise wait until\n"
" being notified in segment <id>\n"
" n <id>: notify the SharedCondition in segment ID <id>\n"
;
return 0;
}
switch (argv[1][0])
{
case ’c’:
{
SharedMemory shmem(1, SharedMemory::kB);
SharedCondition cond = SharedCondition::create(shmem);
void *ptr = shmem.ptr();
cout << "ID = " << shmem.id() << ", SharedCondition at " <<
cond.offset() << endl;
break;
}
case ’k’:
{
SharedMemory shmem(stoll(argv[2]));
shmem.kill();
break;
}
case ’m’:
{
SharedMemory shmem(stoll(argv[2]));
SharedCondition cond = SharedCondition::attach(shmem);
cond.lock();
cout << "Obtained the lock. Now waiting for a notification\n";
while (true)
{
switch (cond.wait_for(chrono::seconds(5)))
{
case cv_status::timeout:
cout << "Waited for 5 seconds\n\n";
break;
case cv_status::no_timeout:
cond.unlock();
cout << "Received the notification. Unlocked.\n";
return 0;
}
}
}
case ’w’:
{
SharedMemory shmem(stoll(argv[2]));
SharedCondition cond = SharedCondition::attach(shmem);
cond.lock();
cout << "Obtained the lock. Now waiting for a notification\n";
cond.wait();
cout << "Received the notification. Unlocking.\n";
cond.unlock();
break;
}
case ’n’:
{
SharedMemory shmem(stoll(argv[2]));
SharedCondition cond = SharedCondition::attach(shmem);
cout << "Notifying the other after Enter ";
cin.ignore(1000, ’\n’);
cond.lock();
cout << "Obtained the lock. Now notifying the other\n";
cond.notify();
cout << "Sent the notification. Now unlocking.\n";
cond.unlock();
break;
}
}
}
catch (exception const &exc)
{
cout << "Exception: " << exc.what() << endl;
}
bobcat/sharedcondition - defines the class interface
bobcat(7) isharedstream(3bobcat), osharedstream(3bobcat), sharedblock(3bobcat), sharedmemory(3bobcat), sharedpos(3bobcat), sharedreadme(7bobcat), sharedsegment(3bobcat), sharedstream(3bobcat), sharedbuf(3bobcat)
None Reported.
Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.
This is free software, distributed under the terms of the GNU General Public License (GPL).
Frank B. Brokken (f.b.brokken@rug.nl).
| 2005-2020 | libbobcat-dev_5.07.00 |