Ejemplo n.º 1
0
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;
}