Robotics StackExchange | Archived questions

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

I am able to use pthread and create a separate thread, however when I use pthreadcondtimedwait 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) { pthreadcreate(&(this->pt1), NULL,recieve_packet,NULL); // Interferes with UDP in pose2sim plugin in gazebo this->flag=0; } else {

    mywait(10);
}

pthreadmutext fakeMutex = PTHREADMUTEXINITIALIZER; pthreadcondt fakeCond = PTHREADCONDINITIALIZER;

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");

}

Asked by pchidamb on 2020-03-12 13:15:07 UTC

Comments

Answers