0

I am using Toradex Colibri iMX7 for running our embedded software(C,C++). Our application is to acquire data from two sensors and to plot it in real time.We are having two threads, one for data acquisition(append data in a global array) and other thread for plotting the array of values(same global array) in a interval of time(100ms). While trying this our application gets crashed after some time. I know some kind of thread synchronization is necessary but don't know exactly how to handle this. Any suggestions or examples would be helpful.

3
  • 2
    Mulit-threade reader-writer problems and synchronization is an old and long solved problem. There are plenty of documentation and tutorials both in books as well as all over the Internet. What have you searched for? What have you found? What have you tried? Commented Aug 10, 2020 at 4:32
  • Check out "posix thread mutex" on google. here's something to get you started: cs.cmu.edu/afs/cs/academic/class/15492-f07/www/pthreads.html Commented Aug 10, 2020 at 4:33
  • Another helpful link POSIX Thread Programming Commented Aug 10, 2020 at 4:48

1 Answer 1

1

Here is a dummy example how to use mutex for thread synchronization with pthread library.

#include <pthread.h>
pthread_mutex_t _mutex; 
int globalArray[5];

void Write()
{
    pthread_mutex_lock (&_mutex);  

    // Write to global array
    globalArray[0] = 0;
    
    pthread_mutex_unlock (&_mutex);
}

int Read( )
{
    int i;
    pthread_mutex_lock (&_mutex);  
    
    // read from global array
    i = globalArra[0];

    pthread_mutex_unlock (&_mutex);
    return i;
}

Before you start using mutex object one time initialization needed. eg. begining of your program.

pthread_mutex_init(&_mutex, NULL);

and when no longer need it you need to destroy it. eg. before program ends.

pthread_mutex_destroy(&_mutex);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your brief explanation.It helped a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.