Solved

How to suspend/resume a Posix thread

Posted on 2002-03-18
5
1,669 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 300 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
shell script help 1 151
understanding folder structure inside an rpm 2 74
making an rpm that just copies the entire project 2 56
improve Shell script 5 155
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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