Solved

How to suspend/resume a Posix thread

Posted on 2002-03-18
5
1,672 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…

726 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