2005820037 发表于 2012-7-26 19:43

C++线程问题

以前没学过c++,最近开始在看android的代码,代码如下,这个thread中有个do while循环,这个循环是不是一直在进行啊?正常sensor轮询读取数据应该不用这么快吧?

bool SensorService::threadLoop()
{
    LOGD("nuSensorService thread starting...");
    const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
    sensors_event_t buffer;
    sensors_event_t scratch;
    SensorDevice& device(SensorDevice::getInstance());
    const size_t vcount = mVirtualSensorList.size();
    ssize_t count;
    do {
      count = device.poll(buffer, numEventMax);
      if (count<0) {
            LOGE("sensor poll failed (%s)", strerror(-count));
            break;
      }
      recordLastValue(buffer, count);
      // handle virtual sensors
      if (count && vcount) {
            const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
                  getActiveVirtualSensors());
            const size_t activeVirtualSensorCount = virtualSensors.size();
            if (activeVirtualSensorCount) {
                size_t k = 0;
                for (size_t i=0 ; i<size_t(count) ; i++) {
                  sensors_event_t const * const event = buffer;
                  for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
                        sensors_event_t out;
                        if (virtualSensors.valueAt(j)->process(&out, event)) {
                            buffer = out;
                            k++;
                        }
                  }
                }
                if (k) {
                  // record the last synthesized values
                  recordLastValue(&buffer, k);
                  count += k;
                  // sort the buffer by time-stamps
                  sortEventBuffer(buffer, count);
                }
            }
      }
      // send our events to clients...
      const SortedVector< wp<SensorEventConnection> > activeConnections(
                getActiveConnections());
      size_t numConnections = activeConnections.size();
      for (size_t i=0 ; i<numConnections ; i++) {
            sp<SensorEventConnection> connection(
                  activeConnections.promote());
            if (connection != 0) {
                connection->sendEvents(buffer, count, scratch);
            }
      }
    } while (count >= 0 || Thread::exitPending());
    LOGW("Exiting SensorService::threadLoop!");
    return false;
}

sinanjj 发表于 2012-7-27 23:21

不会c++的飘过。

2005820037 发表于 2012-7-30 08:44

路过,看看楼上
页: [1]
查看完整版本: C++线程问题