Sunday, 15 March 2015

c++ - how to wait effectively on a set of semaphore? -


I am using Sakers with shared-memory to communicate between multi-producer and multi-client There are two main types of semaphores in the system, which are "stored semaphores" and "processed semaphores"

The system is running as following: Producers constantly put data in shared-memory, and then stored Increase the value of SACAFORE, while consumers are in the loop To wait for such stored Simantikarn. Consumers will process such data after receiving data from the manufacturer, and then, the increase in the value of processed semaphores will result in producers waiting on "processed semaphore"

Manufacturer Code:

 for  (int i = 0; i & lt; nloop; i ++) {sleep (100); Strcpy (shared_mem [i], "data for processing"); Sem_post (& amp; shared_mem [i] .stored_semaphored); If (sem_timedwait (& msg_ptr-> processed_symph, & amp; ts) == - 1) {// wait for the result if (error == ETiIdutty} {} break; } Else {// success}}   

Consumer Code:

 for  (int j = 0; j & lt; MAX_MESSAGE; j ++) {If (Sem_trywait (& amp; (shm_ptr-> Message [j]. Stored_semaphore)) == -1) {if (errno == EAGAIN) {} else {// success ==> Process data // post results in shared memory, and increased // ProSide by StraCopy (shared_mum [j] .output, "processed data"); Sem_post (& amp; (shared_mem [ja] .processed_semaphore)); }}}} MAX_MESSAGE   

My problem is that almost 100% of CPU is being wasted for loop in the consumer code because there is no data from the manufacturer, this loop To run continuously.

My question is that there is no other way to wait on the set of SACPSAR (which can be similar to the waiting mechanism by selection, pole, or EPOL), which does not ruin the CPU time.

Hope you see your answer. Thanks a lot!

As far as I know there is no way to wait on a set of semaphores. This means that all accesses should be done online through a single semaphore. You are looping on a set of sakers, so they can become collectively an object. The need to know the consumer when any semaphores are indicated, to use an additional sem_post on a new semaphore to indicate that the set of semaphores has changed.

Your manufacturer code becomes something like this:

  .... sem_post (and shared_emam [i]. Stored_semaphored); Sem_post (& amp; list_changed_semaphore); / * Consumers wake * / ....   

and consumer:

  / * Unless a user has indicated that he changed the Sackfire list / / (If! Sem_wait (& amp; list_changed_semaphore)) at least one manufacturer has changed a signal * / for (int j = 0; j & lt; MAX_MESSAGE; J ++) { If (sem_trivite (and (mmtp-> message [ja] .store_symer)) == -1) {}}}   

list_changed_semaphore for one Instead of using semaphore you need to have something in your set of sigma You can use a pthread_cond_t position variable for the prompting of the logging list_changed_semaphore as the example shown here does not need to be a counter, to indicate it There is a need to be only a bit that a manufacturer has revised the list.

No comments:

Post a Comment