Can I run more than one instance of a thread?

dibosco wrote on Wednesday, January 13, 2016:

Is it possible to create a generic thread that you create multiple times, but with a different passed parameter and have those tasks run independently?

Specificially I have a thread that opens a file on an SD card, reads info off the file in small chunks and sends it to another thread to process. I’d really like to be able to create that thread up to six times, each time passing the path to file on the SD card to open as the thread’s parameter, then that thread pass values back to another thread via a queue. So, I could have a few files open at the same time with each instance of the SD card reading thread passing information via the [same] queue.

Once I’d finished with it, I’d then destroy the thread.

Is that possible/sensible/stupid?

Thanks!

Rob

rtel wrote on Wednesday, January 13, 2016:

Yes, definitely possible, but watch out for thread safety issues when accessing the SD card. Are you using our FAT file system? If so, I think the Windows demo does just that.

As a very simple example, you could look at the LED flash standard demo (although the file is very old!). There is only one task implemented, but many instances of the task. This works because each task has its own context - its own stack and its own [virtua] set of CPU register.

dibosco wrote on Wednesday, January 13, 2016:

I’m using the Chan FS, I didn’t even know you had one as well!

I used a mutex to ensure only one thread was accessing the libraries at any time and ensure safety and it seems to be working nicely. :slight_smile: