start_process() and memory access issues

3 replies [Last post]
garrettsickles
garrettsickles's picture
Title: NooBot
Joined: 12/08/2011
Posts:
BotPoints: 52
User offline. Last seen 9 years 2 weeks ago.

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

Garrett Sickles
807Robotics: A Narwhal's Revenge
Youth Advisory Council
Geophysics & Computer Science

Beta
Beta's picture
Title: The Magnificent
Joined: 02/24/2012
Posts:
BotPoints: 266
User offline. Last seen 9 years 6 weeks ago.

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:

  1. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  2. unsigned int i = 0;
  3.  
  4. void my_process() {
  5. while(1) {
  6. pthread_mutex_lock(&mutex);
  7. i++;
  8. pthread_mutex_unlock(&mutex);
  9. }
  10. }
  11.  
  12. int main() {
  13. while(1) {
  14. pthread_mutex_lock(&mutex);
  15. printf("%u\n", i);
  16. pthread_mutex_unlock(&mutex);
  17. }
  18. }

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.

garrettsickles
garrettsickles's picture
Title: NooBot
Joined: 12/08/2011
Posts:
BotPoints: 52
User offline. Last seen 9 years 2 weeks ago.

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

garrettsickles
garrettsickles's picture
Title: NooBot
Joined: 12/08/2011
Posts:
BotPoints: 52
User offline. Last seen 9 years 2 weeks ago.

A function like this could be helpful if included in your wrapper

  1. int start_nvprocess(void (*function_name)(), void *data_ptr)
  2. {
  3. pthread_t this_thread;
  4. return(pthread_create(&this_thread, NULL, function_name, (void *) data_ptr));
  5. }

Garrett Sickles
807Robotics: A Narwhal's Revenge
Youth Advisory Council
Geophysics & Computer Science