Hi,
I have a multi-threaded application. I have a situation where I would like
to suspend a thread from another, perform some operation then resume the
original thread after completion ?
What is the best way to do this?
Thank you,
–
Simon Platten, Senior Software Engineer
VT Controls
Bessemer Way, Harfreys Industrial Estate, Great Yarmouth, Norfolk, NR31 0LX.
Tel: (0)1493 668811 Fax: (0)1493 651137
Email: simon.platten@vtcontrols.co.uk
Any views or opinions expressed are solely those of the author and do not
necessarily
represent those of VT Group, its holding company or any of its subsidiaries
or associates.
Simon A. Platten <simon.platten@vtcontrols.co.uk> wrote:
Hi,
I have a multi-threaded application. I have a situation where I would like
to suspend a thread from another, perform some operation then resume the
original thread after completion ?
What is the best way to do this?
I’ve talked about this issue before – I don’t think there is any
easy or clear way of doing this.
Will pre-emption be sufficient? If your thread is a higher priority
than the other thread, and you don’t make any blocking calls in the
“perform some operation”, then (on a non-SMP platform) you can know
that the other thread won’t run while you are running.
Otherwise, this can get very messy, and one can get into some very
sticky issues of safety of data – are you using a mutex to protect
shared data? What happens if you suspend a thread, then try to
access the shared data…so you go to lock the mutex, but the
suspended thread also has it locked?
Hm… playing with the other thread’s priority might work… create
a priority 2 thread that never blocks [while(1);], then to “suspend”
the thread, set it to priority 1, and to “resume” the thread, return
it to its former priority. Possible issues: priorities of threads
can be changed due to mutex contention, IPC and other stuff.
-David
QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.
Hi Simon,
Did you consider using a mutex. I believe your thread must have a loop.
Somewhere inside this loop you can call pthread_mutex_lock(). When you need
to suspend the thread just lock the mutex from outside.
Sincerely,
Serge
“Simon A. Platten” <simon.platten@vtcontrols.co.uk> wrote in message
news:aribbq$l0s$1@inn.qnx.com…
Hi,
I have a multi-threaded application. I have a situation where I would
like
to suspend a thread from another, perform some operation then resume the
original thread after completion ?
What is the best way to do this?
Thank you,
–
Simon Platten, Senior Software Engineer
VT Controls
Bessemer Way, Harfreys Industrial Estate, Great Yarmouth, Norfolk, NR31
0LX.
Tel: (0)1493 668811 Fax: (0)1493 651137
Email: > simon.platten@vtcontrols.co.uk
Any views or opinions expressed are solely those of the author and do not
necessarily
represent those of VT Group, its holding company or any of its
subsidiaries
or associates.
Hi,
The first thing I tried was actually a mutex and this works well, providing
that none of the routines used in the thread have loops. It doesn’t suspend
the thread immediately, if you have any delays or sleeps in the thread it
won’t work, which I do. I tend to use mutexes for data synchronisation only
as I’ve had real trouble when trying to use them for locking sections of
code, cases where they just become permanently locked due to errors or
cancellations.
The Momentics IDE has a menu option to suspend a thread, I would like to
know how this is done. I have developed multi-threaded applications in
Windows which do have a suspend and resume feature.
Regards,
Simon
“Simon A. Platten” <simon.platten@vtcontrols.co.uk> wrote in message
news:aribbq$l0s$1@inn.qnx.com…
Hi,
I have a multi-threaded application. I have a situation where I would
like
to suspend a thread from another, perform some operation then resume the
original thread after completion ?
What is the best way to do this?
Thank you,
–
Simon Platten, Senior Software Engineer
VT Controls
Bessemer Way, Harfreys Industrial Estate, Great Yarmouth, Norfolk, NR31
0LX.
Tel: (0)1493 668811 Fax: (0)1493 651137
Email: > simon.platten@vtcontrols.co.uk
Any views or opinions expressed are solely those of the author and do not
necessarily
represent those of VT Group, its holding company or any of its
subsidiaries
or associates.
How do you set/change the priority of threads within a process. I’ve
searched the help and as far as I can see there are routines that apply for
changing the priority of the process, but I couldn’t see any that were
specific to threads within a process.
In theory this idea sounds good.
“David Gibbs” <dagibbs@qnx.com> wrote in message
news:arj510$kft$1@nntp.qnx.com…
Simon A. Platten <> simon.platten@vtcontrols.co.uk> > wrote:
Hi,
I have a multi-threaded application. I have a situation where I would
like
to suspend a thread from another, perform some operation then resume the
original thread after completion ?
What is the best way to do this?
I’ve talked about this issue before – I don’t think there is any
easy or clear way of doing this.
Will pre-emption be sufficient? If your thread is a higher priority
than the other thread, and you don’t make any blocking calls in the
“perform some operation”, then (on a non-SMP platform) you can know
that the other thread won’t run while you are running.
Otherwise, this can get very messy, and one can get into some very
sticky issues of safety of data – are you using a mutex to protect
shared data? What happens if you suspend a thread, then try to
access the shared data…so you go to lock the mutex, but the
suspended thread also has it locked?
Hm… playing with the other thread’s priority might work… create
a priority 2 thread that never blocks [while(1);], then to “suspend”
the thread, set it to priority 1, and to “resume” the thread, return
it to its former priority. Possible issues: priorities of threads
can be changed due to mutex contention, IPC and other stuff.
-David
QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.
Simon A. Platten <simon.platten@vtcontrols.co.uk> wrote:
How do you set/change the priority of threads within a process. I’ve
searched the help and as far as I can see there are routines that apply for
changing the priority of the process, but I couldn’t see any that were
specific to threads within a process.
pthread_setschedparam()
Another possible bludgeon:
ThreadCtl( _NTO_TCTL_THREADS_HOLD, 0);
will freeze all threads in the process EXCEPT the calling thread, and
ThreadCtl( _NTO_TCTL_THREADS_CONT, 0);
will unfreeze all the threads that were frozne by the HOLD ThreadCtl.
-David
QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.
Simon A. Platten <simon.platten@vtcontrols.co.uk> wrote:
Hi,
The first thing I tried was actually a mutex and this works well, providing
that none of the routines used in the thread have loops. It doesn’t suspend
the thread immediately, if you have any delays or sleeps in the thread it
won’t work, which I do. I tend to use mutexes for data synchronisation only
as I’ve had real trouble when trying to use them for locking sections of
code, cases where they just become permanently locked due to errors or
cancellations.
If you have permanently locked mutexes it generally points to bugs in
your code. If a thread could be cancelled while it has a mutex locked,
it should have a cancellation handler setup to unlock the mutex (and
restore the data protected by the mutex to a consistent state).
The Momentics IDE has a menu option to suspend a thread, I would like to
know how this is done.
The IDE is not purely QNX oriented – we inheritted much of the
interface and architecture of it from the Eclipse framework. Still, where
did you see a “suspend a thread” menu? If you are talking about the
debugger control view, where you can click on a thread, and select
suspend/resume, you will find that it actually suspends/resumes all
the threads in the process.
I have developed multi-threaded applications in Windows which do have
a suspend and resume feature.
Yes, some other operating systems can/do have this feature. Like thread
cancellation, it can cause some real messy issues…such as how to lock
a mutex owned by the suspended thread.
-David
QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.
The suspend / resume menu was in the debugger, I didn’t realise it suspended
the whole application.
The more I think about the problems I’m having with threads in my
application the more I think I would be better off not using them and
instead splitting the application into a series of smaller processes.
Regards,
Simon
“David Gibbs” <dagibbs@qnx.com> wrote in message
news:arlnon$air$1@nntp.qnx.com…
Simon A. Platten <> simon.platten@vtcontrols.co.uk> > wrote:
Hi,
The first thing I tried was actually a mutex and this works well,
providing
that none of the routines used in the thread have loops. It doesn’t
suspend
the thread immediately, if you have any delays or sleeps in the thread
it
won’t work, which I do. I tend to use mutexes for data synchronisation
only
as I’ve had real trouble when trying to use them for locking sections of
code, cases where they just become permanently locked due to errors or
cancellations.
If you have permanently locked mutexes it generally points to bugs in
your code. If a thread could be cancelled while it has a mutex locked,
it should have a cancellation handler setup to unlock the mutex (and
restore the data protected by the mutex to a consistent state).
The Momentics IDE has a menu option to suspend a thread, I would like to
know how this is done.
The IDE is not purely QNX oriented – we inheritted much of the
interface and architecture of it from the Eclipse framework. Still, where
did you see a “suspend a thread” menu? If you are talking about the
debugger control view, where you can click on a thread, and select
suspend/resume, you will find that it actually suspends/resumes all
the threads in the process.
I have developed multi-threaded applications in Windows which do have
a suspend and resume feature.
Yes, some other operating systems can/do have this feature. Like thread
cancellation, it can cause some real messy issues…such as how to lock
a mutex owned by the suspended thread.
-David
QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.
Simon A. Platten <simon.platten@vtcontrols.co.uk> wrote:
The suspend / resume menu was in the debugger, I didn’t realise it suspended
the whole application.
The more I think about the problems I’m having with threads in my
application the more I think I would be better off not using them and
instead splitting the application into a series of smaller processes.
That can make a lot of sense – make it far easier to isolate
interactions, and make sure different pieces don’t conflict
with each other.
-David
QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.