//// initialize the shared memory objects
void testSharedMemTool::initializeSharedMemory()
{
    boost::interprocess::shared_memory_object::remove("mmDaqSharedMemory");
    boost::interprocess::named_condition::remove("mmDaqSharedCondition");
    boost::interprocess::named_mutex::remove("mmDaqSharedMutex");

    if(!m_shm_manager)
        m_shm_manager = new boost::interprocess::managed_shared_memory(boost::interprocess::open_or_create,
                                                                       "mmDaqSharedMemory",
                                                                       (104857600 * 10) );

    if(!m_shm_condition)
        m_shm_condition = new boost::interprocess::named_condition(boost::interprocess::open_or_create,
                                                                   "mmDaqSharedCondition");
    m_shm_eventNumber = m_shm_manager->find_or_construct<uint64_t>("mmDaqSharedEventNumber")(0);

    m_shm_eventinfo_vector = m_shm_manager->construct<ShmemCharStringVector>
            ("mmDaqSharedEventData")
            (m_shm_manager->get_allocator<ShmemCharVectorAllocator>());

    boost::interprocess::named_mutex shm_mutex(boost::interprocess::open_or_create, "mmDaqSharedMutex");
    boost::interprocess::scoped_lock< boost::interprocess::named_mutex > lock(shm_mutex,
                                                                              boost::interprocess::defer_lock_type());


}
void testSharedMemTool::sendData()
{
    std::cout << "testSharedMemTool::sendData" << std::endl;

    boost::interprocess::named_mutex shm_mutex(boost::interprocess::open_or_create, "mmDaqSharedMutex");
    boost::interprocess::scoped_lock< boost::interprocess::named_mutex > lock(shm_mutex,
                               boost::interprocess::defer_lock_type());
            //    m_shm_condition->notify_all();

    int n = 0;
    while(n<1) {
        try {
            std::cout << "in try" << std::endl;

            boost::posix_time::ptime timeout(boost::posix_time::second_clock::universal_time() + boost::posix_time::seconds(5));
          //  if(m_shm_condition->timed_wait(lock, timeout)) {
            if(!lock.timed_lock(timeout)) {
                std::cout << " *** DATAWRITER : LOCK TIMED OUT *** " << std::endl;
            }
            else {
                std::cout << "in else" << std::endl;
                sendEventNumber();
                sendEventInfo();

                m_shm_condition->notify_all();
              //  m_shm_condition->wait(lock);
            }
            //    m_shm_condition->wait(lock);

            if(lock) {
                std::cout << " *** DATAWRITER : unlocking *** " << std::endl;
                lock.unlock();
                std::cout << " *** DATAWRITER : SHM_MUTEX UNLCOKED *** " << std::endl;
            }
        } // try
        catch(boost::interprocess::interprocess_exception &e) {
            std::cout << " *** DATAWRITER : ERROR :: " << e.what() << std::endl;
        }
        n++;
    } // while

    //int n=0;
    //while(n<1000) {
    //    sendEventNumber();
    //    sendEventInfo();
    //    n++;
    //}
    return;
}
void testSharedMemTool::sendData()
{

    boost::interprocess::named_mutex shm_mutex(boost::interprocess::open_or_create, "mmDaqSharedMutex");
    boost::interprocess::scoped_lock< boost::interprocess::named_mutex > lock(shm_mutex,
                                                                              boost::interprocess::defer_lock_type());
    std::cout << "testSharedMemTool::sendData" << std::endl;

    //    m_shm_condition->notify_all();

    int n = 0;
    while(n<100000) {

//        if(n%1000==0)
//        std::cout << "Done: "<< 1000/100000*100 <<"%\n";
        usleep(5000-rand()%1900);

        try {
//            std::cout << "in try" << std::endl;

            boost::posix_time::ptime timeout(boost::posix_time::second_clock::universal_time() + boost::posix_time::seconds(5));
            //  if(m_shm_condition->timed_wait(lock, timeout)) {
            if(!lock.timed_lock(timeout)) {
//                std::cout << " *** DATAWRITER : LOCK TIMED OUT *** " << std::endl;

                //aikoulou: nah, doesnt work
//                std::cout << " *** TRYING TO FORCE UNLOCK *** " << std::endl;
//                lock.unlock();
            }
            else
            {
                sendEventNumber();
                sendEventInfo();
                m_shm_condition->notify_all();
                //                m_shm_condition->wait(lock);
            }
            //    m_shm_condition->wait(lock);
            if(lock) {
                lock.unlock();
            }
        } // try
        catch(boost::interprocess::interprocess_exception &e) {
            std::cout << " *** DATAWRITER : ERROR :: " << e.what() << std::endl;
        }
        n++;
    } // while
    return;
}