Use of semaphores

Dear All,      In my application,There are lot of variables( more than 200 ) which are shared between the tasks simultaneously.      My question is      Should I use Semaphores or mutexes for each variable ?      If I used this much Semaphores what will be RAM        consumption ?   Sachin D. Bhujbal

Use of semaphores

It depends on many things:   – what is the size of the variables compared to the processor word size?   – will several tasks update those variables?   – do you update each variable independently or do you need to update them at the same time? If all your variables are of type portBASE_TYPE (or equivalent), if they are accessed independently, and if only one task can update them, then you’re probably safe with no locking at all. Otherwise, you can use a mutex (which will bring you priority inheritance) by variable (and yes, it will consume a lot of RAM), or only one for the whole variables set (it will be a giant lock). Clearly, you need to be more precise in your description if you want to obtain useful help.

Use of semaphores

OK,    Actually, my requirement is clearified in one sentence only. We will take a example to make it more clear. consider following arrays of structures, { ucVariable1; usVariable2; usVariable3; ulVariable4; ullVariable5; . . ulVariable30;   }gastStructure1[4] { ucVariable1; usVariable2; usVariable3; ulVariable4; ullVariable5; . . ulVariable30;   }gastStructure2[4] . . . { ucVariable1; usVariable2; usVariable3; ulVariable4; ullVariable5; . . ulVariable30;   }gastStructure15[4] And consider Tasks A, B, C, D, E, F, G Now in my case Task A writes ucVariabel1 in gastStructure1[0]. Task B writes ucVariabel1 in gastStructure1[0]. Task C reads ucVariabel1 in gastStructure1[0]. It is sure that I will require to use Semaphore or Mutex for ucVariable1 in gastStructure1 in Task A & B. But task C only reads it, so do I required to use semaphore in Task C also ? And as explained above each Variable in each structure is shared like that. Your suggestion about using semaphore to group of variables is good but in my application, those variables cannot be grouped, as each variable is dedicated for specific requirement by different tasks. Can there be some other solution for such logic ? Sachin D. Bhujbal

Use of semaphores

Actually your requirements are still not very clear. Why would you need so many variables that are just called "variable", and all for some unrelated purpose? I cannot think of any serious application that needs to do something like that. Data shared by tasks is usually limited to a small (e.g. 1 or 2 or so) number of objects. If there are more than that then the objects are usually passed from one task to the next using a queue, in which case your synchronisation issues are dealt with automatically. Note that with the abstract term "object" I am not referring to a C++ object but some entity, i.e. a collection of values that belong to something larger. It is these objects that you firstly need to define and then you need to protect them as a whole so that their content does not become inconsistent due to a race condition.