Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to suspend/resume a Posix thread

Posted on 2002-03-18
5
Medium Priority
?
1,698 Views
Last Modified: 2007-12-19
I am trying to convert an app from Win32 to Linux.  In so doing I need to emulate the behavior of Win32 threads which allow you to suspend a thread, and resume a thread (these threads can be threads other than the current one).

Example in Win32:

void suspend_this_thread(HANDLE aHandle)
{
     SuspendThread(aHandle);
}

void resume_this_thread(HANDLE aHandle)
{
     ResumeThread(aHandle);
}
0
Comment
Question by:mdlilly
5 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6879205
AFAIK there is no such function 'cause it can simply implemented using signals.
See  man pthread_sigmask  and   man signals.
Means you install a sighandler for your thread, and then simply send it a signal.
0
 
LVL 1

Author Comment

by:mdlilly
ID: 6880202
how do I do that?
0
 
LVL 5

Expert Comment

by:bryanh
ID: 6891258
It's even easier than that.  The SIGSTOP and SIGCONT signals do it by default.

Use the abysmally named kill() function to send a SIGSTOP signal to the thread (which you identify by its pid) and it stops being scheduled.  Ps shows a status of "T".  Send it a SIGCONT and it gets scheduled normally again.

You can use a 'kill' shell command to play with it too.

Be aware that any process owned by the same user can start and stop the thread this way -- not just its parent.
0
 
LVL 5

Accepted Solution

by:
garboua earned 900 total points
ID: 6922787
There is a thread manager class that you can use to control threads, suspend continue, kill etc etc.  You can find it on sourceforge.  You can look at the source code to make sure it is safe first, OR you can suspend thread by making them wait on a semaphore semaphores.
0
 
LVL 1

Author Comment

by:mdlilly
ID: 7194469
I found the semaphore thing did the trick.  It is somewhat limiting since a thread can only suspend itself, not any thread suspend anythread, but that is a limitation I can accept.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question