How can I sync multiple threads to run their loops simultaneously?

0
3
Asked By CodingNinja27 On

I'm working on an application with 4 threads, each running a loop and sleeping for different durations. For example, one thread sleeps for 100 milliseconds and another for 77 milliseconds. I need to find a way for these threads to start their loops at the same time after their sleeps. What's the correct terminology or method to use for this synchronization? I've thought about using a bitset to manage state across threads but am concerned about thread safety. Would using a semaphore around my synchronization logic be a good approach? I'm seeking advice on a reliable way to achieve this synchronization with FreeRTOS on an ESP32, which might not support all typical synchronization primitives.

5 Answers

Answered By TechieGuru99 On

It sounds like what you need is a barrier to synchronize your threads. A barrier allows all threads to wait until they have all reached a certain point before they resume. If FreeRTOS doesn't support barriers, you'll have to create your own solution, which could involve semaphores or condition variables.

AsyncMasterX -

I looked into barriers, but it seems like the ESP32 may not support them as the FreeRTOS framework has limitations. Maybe you could implement a semaphore-based solution instead to achieve a similar result.

Answered By ThreadWhisperer On

Have you considered using a rendezvous approach? If barriers are off the table, a condition variable might help you. You can notify each thread to start once they’ve all reached the sync point. But just a heads-up, condition variables generally notify a single thread, so you might need a way to track all threads' states.

CodingNinja27 -

Right, but since I need all four threads to sync up, maybe tracking their counts with a shared integer would work. I could increment at the start and decrement at the end and only allow them to continue if they're all back to zero.

Answered By MutexHustler On

What you might need is some kind of synchronization lock, like a mutex, to manage thread access and keep your logic from becoming overly complicated. It could simplify your implementation rather than handling everything with bitsets and sleeps.

CodingNinja27 -

True, using a mutex might provide a cleaner solution, especially since the threads have different sleep durations.

Answered By SynchronizeIt On

Just keep in mind that perfect synchronization isn’t guaranteed, especially on the ESP32 as it typically has a dual-core setup. Getting all four threads to start at the exact same moment may be more complex by nature of how thread scheduling works, but you can aim for them to be close, like within 10 milliseconds or so.

CodingNinja27 -

Yeah, that’s definitely my goal! I want them to operate within a reasonable sync to avoid chaos in controlling the solenoids.

Answered By SyncMasterX On

A simpler method could be to assign each thread their own unique memory location and check those values before running their loops. This way, they can wait until all required conditions are met before proceeding.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.