I cannot figure out why, but I cannot seem to directly access a structure or even a global variable from a function that is run with the start_process() function. Everything compiles, and then I run it on the CBC and it crashes. Debugging shows it crashes when I try to access these particular variables. Anyone else had this problem?
-Also having the same problem passing pointers
Accessing global variables from a separate thread will cause nondeterministic behavior if not synchronized. Basically, one thread is writing to a variable at exactly the same time another thread is reading from a variable, which causes your program to crash. This is because accessing and mutating variables are actually not atomic operations in most cases. You'll need to look into Mutual Exclusion. To use mutexes on the CBC, you'll need to
#include <pthread.h>
, which is the threading system the CBC uses. Here is an page containing several examples: http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html.Basically:
Assuming you started the
my_process
thread at some point, this would be safe code. Now, this would probably never crash the program, because you aren't doing anything important with the value. When you start using real variables to do real things, however, crashes can start occurring.I find it very strange we don't offer some sort of pthread_mutex wrapper. I'll add it to my todo list.
Let me know if you need any more help.
Braden McDorman
Developer of the KIPR Link, KISS IDE, KIPR's 2D Simulator, and CBCJVM.
Reach me at bmcdorman(cat)kipr(dog)org where (cat)=@ and (dog)=. if you need assistance of any kind.
Okay! Thanks Braden!
I just utilized the pthread library and it's working fine, especially because it allows you to pass pointers of whatever you please.
VERY HAPPY to have threading working finally!
Garrett Sickles
807Robotics: A Narwhal's Revenge
Youth Advisory Council
Geophysics & Computer Science
A function like this could be helpful if included in your wrapper
Garrett Sickles
807Robotics: A Narwhal's Revenge
Youth Advisory Council
Geophysics & Computer Science