I’ve done a bunch of multi-threaded code. I’m using POSIX threads for my robotic application. I believe I have 6 or 7 threads at the moment. I’m using message queues for communication between my threads.
You aren’t very specific about what is going wrong so all I can offer is some general comments.
- Are you sure you need multiple threads? Again, it’s not clear from your description. Perhaps you’ve done the analysis and have determined that it’s a necessary and useful approach to solving your problem.
- If both threads are accessing a shared resource (i.e. the “bin” you mention) you need some sort of access control, such as a mutex or semaphore.
I’d be happy to look at your code. I’m heading out of town in about an hour, so the first chance I’d have to look at it is next Tuesday.
On Behalf Of Peck Hui Koh
Sent: Thursday, April 17, 2008 9:07 AM
Subject: [Gumstix-users] Need advice on multithreading in Gumstix betweenADC via SPI and storing data in CF card
I have been trying to
learn POSIX threads programming for my Gumstix application - to receive data
from an 16-bit ADC via SPI and store the samples to a log file, with both
processes running in parallel.
I've succeed in trying to make the two processes running simultaneously using pthread in C. I've used a binning approach to store the data i.e. to transfer the read data buffer to the write data thread whenever the sample bin is full. However, I've got a problem after I saw the results from the log file - there is a time delay between reading the samples and writing the data to the file. I'm using the fwrite command for the writing (which should be acceptable in speed). I've also tried using the memcpy command to transfer the read data buffer to a new data buffer for writing, but it could not resolve the delay problem. I've implemented the flags to "communicate" between the read and write threads.
I’m willing to send the C program to anyone who is interested to have a look at it. I'll greatly appreciate if anyone can enlighten me on what are the possible mistakes I could have made and if there is a better/smarter way of dealing with multithreading (if not using pthread).
Can I ask if anyone has tried using multithreading for their application and are willing to share with me their experience? Any comments from robotic enthusiasts will be valued, as I believe this is a common issue where one tries to receive and process the sensor data. I'm dealing with the ADC which can receive/store the data at sampling rate of 7kHz.