Semaphore usage?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 13/07/2010 at 08:06, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   11.5 
    Platform:   Windows  ; Mac  ;  Mac OSX  ; 
    Language(s) :     C++  ;

    ---------
    Hi,

    I have some mp threads that are modifying the same data structure (let´s say a class member container) so I would like to lock the access to avoid inter-thread issues. I assume I can use the c4d semaphore?

    Example:

    control thread calls mp threads and each mp thread pushes values into the same container. Of course this appending will most probably interfere with other threads trying to append at the same time.

    Where do I define the semaphore? in the class the container is a member of or directly in the mp thread? I tried to do it directly in the mp threads but that doesn´t seem to work. Or do I need to use something else? What are my options?

    Thanks in advance



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 13/07/2010 at 21:58, xxxxxxxx wrote:

    in your class container, i would say.
    this is how i do it:

      
    class SomeClass   
    {   
         public:   
              SomeClass() {   
                   lock = Semaphore::Alloc();   
              };   
                 
              ~SomeClass() {   
                   Semaphore::Free(lock);   
              };   
      
              void SomeFunction()   
              {   
                   lock->LockAndWait((Thread* )NULL);   
                      
                   //do stuff that needs doing   
                      
                   lock->UnLock();   
              }   
      
         private:   
              Semaphore* lock;   
    };   
    



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 13/07/2010 at 22:03, xxxxxxxx wrote:

    Oh and dont forget to unlock if your function exits somwhere in the middle... deadlocks are annoying :)

      
              void SomeOtherFunction()   
              {   
                   lock->LockAndWait((Thread* )NULL);   
      
                   //do stuff that needs doing   
      
                   if (someCondition)   
                   {   
                        lock->UnLock();   
                        return;   
                   }   
      
                   //do more stuff that needs doing   
      
                   lock->UnLock();   
              }   
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 14/07/2010 at 01:09, xxxxxxxx wrote:

    thanks fused. That´s actually how I tried it already, as a class member, but it didn´t work. I still got corrupted memory. Hmm, I must have missed something if you also suggest to do it like this. I´ll try again.

    thank you for your feedback



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 14/07/2010 at 03:30, xxxxxxxx wrote:

    Yes, got it now. I checked and checked the code and it all seemed fine (well, not much code anyway), I thought already I was going crazy. The problem was on my side though, I was calling the wrong function! I have an mp function and a single core version of it and the control thread called the single core version all the time so the locked function was actually never called! *slaponforehead*

    Thank you. It seems all to work as it should now. :)


Log in to reply