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.
-
2Mulit-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?Some programmer dude– Some programmer dude2020-08-10 04:32:42 +00:00Commented 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.htmlAndy– Andy2020-08-10 04:33:07 +00:00Commented Aug 10, 2020 at 4:33
-
Another helpful link POSIX Thread ProgrammingDavid C. Rankin– David C. Rankin2020-08-10 04:48:42 +00:00Commented Aug 10, 2020 at 4:48
Add a comment
|
1 Answer
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);
1 Comment
Naai Sekar
Thank you very much for your brief explanation.It helped a lot