threading question - Linux

This is a discussion on threading question - Linux ; I have a user space app that has several "threadCode" threads. In the Main thread, FnA must execute exclusive of a part of the loop in the thread code. For each of the threads, they must never block one another, ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: threading question

  1. threading question

    I have a user space app that has several "threadCode" threads.
    In the Main thread, FnA must execute exclusive of a part of the loop in the
    thread code. For each of the threads, they must never block one another,
    only FnA entry should cause the block of the threads, and then it should wait
    in FnA until all the threads are at a point outside that small code block in
    the threadCode.
    If i could test a mutex without locking it I can do this, but i think any test
    of a mutex locks the mutex if its successful. Another thought i had was to
    use 2 flags. 1 flag says "Im in FnA", the other says "wait, the threadCode
    section is busy". The thing with the 2 flags is though, it sounds like a
    deadlock waiting to happen.

    Main thread
    *
    1:
    *
    "call FnA", its ok if the thread code below
    causes FnA to stall for a few seconds.
    FnA must execute exclusive of a section
    of the code in the threadCode loop
    *
    *
    * Loop 1:

    threadCode(multiple identical copies running)
    *
    *
    2:
    *
    *
    if Main thread is in FnA spin here
    +=
    += If any thread is in this block, FnA must wait
    += until they all have exited this block
    += A threadCode thread must never block another
    += threadCode thread
    *
    *
    *
    *loop 2:



  2. Re: threading question

    Eric writes:

    > I have a user space app that has several "threadCode" threads. In
    > the Main thread, FnA must execute exclusive of a part of the loop in
    > the thread code. For each of the threads, they must never block one
    > another, only FnA entry should cause the block of the threads, and
    > then it should wait in FnA until all the threads are at a point
    > outside that small code block in the threadCode.
    > If i could test a mutex without locking it I can do this, but i
    > think any test of a mutex locks the mutex if its successful. Another
    > thought i had was to use 2 flags. 1 flag says "Im in FnA", the other
    > says "wait, the threadCode section is busy". The thing with the 2
    > flags is though, it sounds like a deadlock waiting to happen.


    I have no idea what FnA is supposed to mean, but from your description
    is sounds like read-write locks might be what you need. Look at the
    pthread_rwlock_* functions.

    --
    Måns Rullgård
    mans@mansr.com

  3. Re: threading question

    Eric wrote:

    > I have a user space app that has several "threadCode" threads.
    > In the Main thread, FnA must execute exclusive of a part of the loop in the
    > thread code. For each of the threads, they must never block one another,
    > only FnA entry should cause the block of the threads, and then it should wait
    > in FnA until all the threads are at a point outside that small code block in
    > the threadCode.


    It sounds like you want a plain ordinary mutex.

    > If i could test a mutex without locking it I can do this, but i think any test
    > of a mutex locks the mutex if its successful.


    A test that didn't lock if successful would be useless. The return
    value would be meaningless by the time you got it. If you don't want
    two threads in a particular place at a particular time, then whichever
    one gets there first has to lock the other one out. There is no other
    way.

    > Another thought i had was to
    > use 2 flags. 1 flag says "Im in FnA", the other says "wait, the threadCode
    > section is busy". The thing with the 2 flags is though, it sounds like a
    > deadlock waiting to happen.
    >
    > Main thread
    > *
    > 1:
    > *
    > "call FnA", its ok if the thread code below
    > causes FnA to stall for a few seconds.
    > FnA must execute exclusive of a section
    > of the code in the threadCode loop
    > *
    > *
    > * Loop 1:
    >
    > threadCode(multiple identical copies running)
    > *
    > *
    > 2:
    > *
    > *
    > if Main thread is in FnA spin here
    > +=
    > += If any thread is in this block, FnA must wait
    > += until they all have exited this block
    > += A threadCode thread must never block another
    > += threadCode thread
    > *
    > *
    > *
    > *loop 2:


    I'm not sure I understand what it is you want. Can you state it much
    more precisely. It sounds like you have large number of threads, all
    of which can safely run FnA at the same time and one thread that must
    block all other threads out of FnA while it runs it. If this is
    correct, this what reader/writer locks are for.

    DS

  4. Re: threading question



    On May 11, 12:30*am, Eric wrote:
    > I have a user space app that has several "threadCode" threads.
    > In the Main thread, FnA must execute exclusive of a part of the loop in the


    Use Mutex for mutual exclusion !

    > thread code. For each of the threads, they must never block one another,
    > only FnA entry should cause the block of the threads, and then it should wait
    > in FnA until all the threads are at a point outside that small code block in

    pthread_cond_signal & pthread_cond_wait
    > the threadCode.
    > If i could test a mutex without locking it I can do this, but i think any test

    pthread_mutex_trylock are you talking ?
    > of a mutex locks the mutex if its successful. Another thought i had was to
    > use 2 flags. 1 flag says "Im in FnA", the other says "wait, the threadCode
    > section is busy". The thing with the 2 flags is though, it sounds like a
    > deadlock waiting to happen.

    Why deadlock ? this is what pthread_mutex_lock and unlock is for !
    They will take care !

    >
    > Main thread
    > *
    > 1:
    > *
    > * "call FnA", its ok if the thread code below
    > * causes FnA to stall for a few seconds.
    > * FnA must execute exclusive of a section
    > * of the code in the threadCode loop
    > *
    > *
    > * Loop 1:
    >
    > threadCode(multiple identical copies running)
    > *
    > *
    > 2:
    > *
    > *
    > if Main thread is in FnA spin here
    > +=
    > += If any thread is in this block, FnA must wait
    > += until they all have exited this block
    > += A threadCode thread must never block another
    > += threadCode thread
    > *
    > *
    > *
    > *loop 2:


    Did not get this



    -Raxit
    www.yourname.mykavita.com
    "World's largest poem collection....Let's create !"

+ Reply to Thread