troubleshooting Question

[C, C++, Boost Thread] Share variables in threads with Ubuntu and C, C++ and Lib Boost

Avatar of ichigokurosaki
ichigokurosakiFlag for Afghanistan asked on
Linux OS DevCC++Robotics
48 Comments3 Solutions1765 ViewsLast Modified:
I'm writing a mobile robot C program which reads data from a serial port to check some sensors and then try to load a joypad to control two servos.

Since i'm using a blocking read to acquire data for sonars from serial port, i can't get input from joypad and from the serial port at the same time, so i can't stop my robot in time if sonars detect a possible collision.

For this reason, after having received few infos from the serial port, I start an infinite while loop to reads possible collisions from the serial port and I start a new thread with Boost library to load and receive input from the joypad.

With this solution, i'm able to use the joypad and receive collisions information at the same time; the problem is that i'm not able to share variable between the previous thread which reads from serial port and the new thread which load the joypad so the previous thread is not able to communicate the the joypad thread if a collision occurs.

I want to do this:

Previous thread detect a collision -> write a value in a linked list -> new thread read it and stop servos.

What can i do in order to let the new thread detect a collision from the previous thread?
I have to use signal() function?

When i try to compile this code, g++ says me that linked list, and in particular, result variable is not declared in the scope.
I think because new thread does not share the old variables and declarations.

int main(){
// do a lots of stuff then..

   s_write(serial_port, "system-start");
   boost::thread collisions_thread1(&robot_move); <-- LOAD JOYPAD IN A NEW THREAD

void check_coll(){


std::string detect_collisions = s_readBlocking(serial_port, TIME_DELAY);

parseCommand(detect_collisions); <-- I SAVE COLLISIONS IN A LINKED LIST


void robot_move(){

// load joypad and then check for collisions

// check for collisions while getting input from joypad
std::map<std::string,  std::string>::const_iterator sonar_collisions = result.find("sonar-collision");
if(sonar_collisions != result.end() && (sonar_collisions->second == "ok"))
  collisions ++;
  std::cout << "STOP!\nCollisions " << sonar_collisions->second << " e count= " << collisions << "\n";

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 3 Answers and 48 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 48 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros