Creating a separate thread in rayplugin onscan function and moving controls between threads

asked 2020-03-12 13:15:18 -0600

pchidamb gravatar image

updated 2020-03-12 13:26:51 -0600

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+1000ULtimeInMs)1000UL; pthread_mutex_lock(&fakeMutex); rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait); pthread_mutex_unlock(&fakeMutex); printf("\nDone\n"); }

edit retag flag offensive close merge delete