简体   繁体   中英

Linux Event Polling not working when using thread

I have the following code to pool event data (based on this Multitouch Protocol ). In order to do so without delay to my application I decided to use threading. The problem is the thread get stuck in the line where it contains:
read(_touchEvent, &ie, sizeof(ie));
I tried putting printf statements around it and its printing prior to it but not after it.

void TouchDriverAdapter::_poolData(){
  // Refer to kernel multitouch.
  struct input_event ie;
  read(_touchEvent, &ie, sizeof(ie));
  if(ie.code == ABS_MT_SLOT){
    _currentSlot = ie.value;
  }else if(ie.code == SYN_REPORT){
    _numContacts = _currentSlot;
    // Transfer cached data to vector.
    for(int i = 0; i <= _numContacts; i++){
      _touchData[i] = _touchDataTemp[i];
    }
  }else{
    if(ie.code == ABS_X){   
      _touchDataTemp[_currentSlot].x = ie.value;
      printf("%i\n", ie.value);
    }else if(ie.code == ABS_Y){
      _touchDataTemp[_currentSlot].y = ie.value;
    }
  }

  _lastCode = ie.code;
}

Heres the singleton that i have that creates the thread.

// Singleton.
TouchDriverAdapter* TouchDriverAdapter::getInstance(){
  if(_instance->_touchEvent < 0){
    return NULL;
  }else if(_running == false){
    int result = pthread_create( TouchDriverAdapter::_thread, NULL, 
                 TouchDriverAdapter::_runThread, 
                 (void*)&_instance);
    if(result != 0){
      printf("Error: %i, Failed to create a thread.\n", result);
      return NULL;
    }
    _running = true;
    pthread_detach( *TouchDriverAdapter::_thread);
    //pthread_join( *TouchDriverAdapter::_thread, NULL);
    return TouchDriverAdapter::_instance;
  }else if(_running == true){
    return _instance;
  }
}

Again this works when I dont use threading but I can only read segments of data at a time which is kinda useless for multi-touch application.

I was using POSIX pthread_t above, a library not written with classes in mind. The C++'s std::thread on the other hand made this event polling work flawlessly, I just have to add -std=c++11.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM