-
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:
-
Re: threading question
Eric <Scorpus@gordinator.org> writes:
[color=blue]
> 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.[/color]
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
[email]mans@mansr.com[/email]
-
Re: threading question
Eric wrote:
[color=blue]
> 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.[/color]
It sounds like you want a plain ordinary mutex.
[color=blue]
> 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.[/color]
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.
[color=blue]
> 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:[/color]
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
-
Re: threading question
On May 11, 12:30*am, Eric <Scor...@gordinator.org> wrote:[color=blue]
> 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[/color]
Use Mutex for mutual exclusion !
[color=blue]
> 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[/color]
pthread_cond_signal & pthread_cond_wait[color=blue]
> the threadCode.
> If i could test a mutex without locking it I can do this, but i think any test[/color]
pthread_mutex_trylock are you talking ?[color=blue]
> 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.[/color]
Why deadlock ? this is what pthread_mutex_lock and unlock is for !
They will take care !
[color=blue]
>
> 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:[/color]
Did not get this :(
-Raxit
[url]www.yourname.mykavita.com[/url]
"World's largest poem collection....Let's create !"