Semaphore Questions



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

    On 08/02/2007 at 05:35, xxxxxxxx wrote:

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

    ---------
    Hi,

    I know that semaphores are i.e. used to synchronise several threads working on the same data. Now, I have a calculation which can be split into "chunks" and I would like to use several threads (one for each CPU available), each processing one of these chunks.

    However, I assume that I will need a semaphore to synchronise the processing of the data, because the thread I am in is in GetVirtualObjects() and my program shall wait until the chunks have been processed until it starts processing other data.

    So my question is, are my assumptions about using the semaphore correct? And will I need only 1 semaphore to use it with several threads? or do I need a semaphore for each thread (sounds useless somehow)?

    Is there a code example available?

    I would highly appreciate any help. Michael W, if you read this, I remember you once worked with semaphores in one of your programs, so any input is also appreciated!

    Thank you in advance



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

    On 08/02/2007 at 06:15, xxxxxxxx wrote:

    Hello,

    I have not used Semaphore until now but
    I am interesting about it too.
    Probably one will need to Lock the data with only one Semaphore and wait in GetVirtualObjects() until it will be UnLock.
    Need to try it and post it it will be work...



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

    On 08/02/2007 at 07:24, xxxxxxxx wrote:

    One question do Semaphore actually needed?

    I have done some test with 2 threads on my Core2 Duo CPU.

      
    LONG          tsize = 10000000;  
    Vector          *tarr = (Vector* )GeAllocNC(sizeof(Vector)*tsize);  
      
    SimTestThread t1(sem,tarr,0,tsize/2);   
    SimTestThread t2(sem,tarr,tsize/2,tsize);  
      
    t1.Start(TRUE,THREADPRIORITY_NORMAL);  
    t2.Start(TRUE,THREADPRIORITY_NORMAL);  
      
    while(t1.IsRunning() || t2.IsRunning()) {}  
      
    GeFree(tarr);  
    

    Every thread do some complex and useless work on tarr[] and while loop wait until the work is done.
    Unfortunately thread stuff is slower as the same work in one loop...
    So probably this is wrong way.
    What is wrong with this way?



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

    On 08/02/2007 at 09:36, xxxxxxxx wrote:

    You only need a semaphore to prevent two threads trying to access the same memory, e.g. one thread sets a pointer whilst another is still using it. You just have one semaphore, then when a thread is about to use some shared memory you lock it, this stops any other threads that also lock the semaphore since its locked so they must wait until the lock is released. Just be careful to always unlock or you have a potential problem. You may also want to read on the spinlocks, they are faster then a full semaphore. Depends what you need. A spinlock is just a block for any access no matter what thread so must be used carefully.



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

    On 10/02/2007 at 06:01, xxxxxxxx wrote:

    Hi and thank you both of you!

    Then my assumption was right and I need a semaphore, because I need access to the same data at the same time! :)

    Great.



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

    On 10/02/2007 at 06:31, xxxxxxxx wrote:

    Well the if you will get it to work can you share how to do it in the best way? :)

    Thanks,
    Remo



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

    On 10/02/2007 at 07:13, xxxxxxxx wrote:

    yep, will do so :)



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

    On 13/02/2007 at 06:38, xxxxxxxx wrote:

    Well now I know my mistake.
    To run thread on different CPU Cores one need to use MPThread!

    Like this...

      
         MPThread mp;  
         SimTestThread thread[MAX_THREADS];  
         Thread* list[MAX_THREADS];  
      
         LONG cnt = GeGetCPUCount();  
         for (LONG i = 0; i < cnt; ++i)  
         {  
          thread _.Init(tarr,i*tsize/cnt,(i+1)*tsize/cnt);  
          list _= &thread; _;  
         }  
         if (!mp.Init(bt, cnt, list)) return 0;  
         if (!mp.Start()) return 0;  
         mp.Wait();  
    


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

    On 13/02/2007 at 07:27, xxxxxxxx wrote:

    Hi Remo!

    Ah I see! Great. thanks for the info. I haven´t had the time to try this yet (due to the network problems) but I´ll try this asap. Looks great and, hehe, actually straight-forward with that MPThread (man sieht den Baum vor lauter Bäumen nicht :D )

    thank you for sharing your results Remo!



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

    On 15/02/2007 at 13:51, xxxxxxxx wrote:

    It seems to be important to use Control thread!

      
    if (!mp.Init(cThread, cnt, list)) return 0;  
    

    In my test inside Execute() without Control thread I got crashes in some cases!


Log in to reply