ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ros::ok() within pthread

asked 2012-02-08 05:14:57 -0600

rplankenhorn gravatar image

updated 2012-02-09 01:03:34 -0600

I am have a couple of threads that run in a node. The threads have a while(ros::ok()) infinite loop inside. The problem is that when I Ctrl-C my program, it has to escalate to a SIGTERM because the ros::ok() variable never goes false. I also tried the ros::isShuttingDown() variable and this never goes true.

How should I handle the pthreads so that they exit when the rest of the program exits?

I launch threads with:

pthread_t thread1
pthread_create(&thread1,NULL,thread1Function,NULL);

My thread function looks something like this:

void *thread1Function(void *obj)
{
ros::Rate loop_rate(1);

while(ros::ok())
{
//Do stuff
loop_rate.sleep();
ros::spinOnce();
}

pthread_exit(NULL);
}

The loop never exits.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
0

answered 2012-02-09 01:42:04 -0600

rplankenhorn gravatar image

Alright I figured it out. I had GDB enabled for the module and it for some reason wasn't killing the threads. As soon as I disabled it in my launch file, the threads get killed when I Ctrl-C the program. Thanks for the suggestions.

edit flag offensive delete link more
1

answered 2012-02-08 05:24:46 -0600

DimitriProsser gravatar image

updated 2012-02-09 01:26:51 -0600

I've never had a problem running ROS with threads. Are you calling ros::spinOnce() to poll for updates?

I've never used pthread, but I know for a fact that it works with boost::thread. I know that doesn't solve your issue, but it's something to consider. But since it's really just a nice wrapper for pthreads on *Nix systems, it doesn't really explain the problem.

EDIT:

Try passing a this pointer to pthread_create(). Since the ros::init() is called in the parent class, you need to have access to the ROS variables in your thread.

edit flag offensive delete link more

Comments

See my sample code above.
rplankenhorn gravatar image rplankenhorn  ( 2012-02-09 01:03:57 -0600 )edit
0

answered 2012-02-08 13:58:42 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

You need to provide ros a thread to use. The standard way to do this is to call ros::spin() at the bottom of your main thread.

For more information, see the threading section of the roscpp Overview

edit flag offensive delete link more

Comments

I do this in the main thread. I launch individual threads and then drop into a while(ros::ok()) loop that exits fine when I do Ctrl-C.
rplankenhorn gravatar image rplankenhorn  ( 2012-02-09 01:04:48 -0600 )edit

Question Tools

Stats

Asked: 2012-02-08 05:14:57 -0600

Seen: 3,243 times

Last updated: Feb 11 '12