I am able to use pthread and create a separate thread, however when I use pthread_cond_timedwait I am unable to consistently move to between the main thread - onscan and the thread function. Why is this?
void GazeboRosVelodyneLaser::OnScan(ConstLaserScanStampedPtr &_msg) { if (this->flag ==1) { pthread_create(&(this->pt1), NULL,recieve_packet,NULL); // Interferes with UDP in pose2sim plugin in gazebo this->flag=0; } else {
mywait(10);
}
pthread_mutex_t fakeMutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t fakeCond = PTHREAD_COND_INITIALIZER;
void mywait(int timeInMs) { struct timespec timeToWait; struct timeval now; int rt;
gettimeofday(&now,NULL);
timeToWait.tv_sec = now.tv_sec+5;
timeToWait.tv_nsec = (now.tv_usec+1000UL*timeInMs)*1000UL;
pthread_mutex_lock(&fakeMutex);
rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait);
pthread_mutex_unlock(&fakeMutex);
printf("\nDone\n");
}