variable has different values inside a thread and main loop

asked 2016-01-08 14:04:00 -0600

I'm trying to read some data from TCP socket, and publish it via advertise. I'm reading from TCP socket in a boost::thread and store my data in a char buf[255]:

read(newsockfd, buf, 255);

now I publish my message like this:

ros::NodeHandle ethernet_node;
ros::Publisher ethernet_publish;
ethernet_publish = ethernet_node.advertise<std_msgs::string>("/info"  , 10);
and inside my thread:
std_msgs::String msg;
msg.data = buf;
ethernet_publish.publish(msg);

The problem is that if I do the publish in the thread, the message doesn't even get published. If I call the publish within my main loop, the message gets published but buf value hasn't changed and it still has the initial value. But when I print it inside my thread right after I store the read data in it, it shows the received data correctly. It seems that there are two instances of the same variable one inside my thread and one inside my main loop!! Can anyone help?

edit retag flag offensive close merge delete