Solved

How to suspend/resume a Posix thread

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IPtables not running - RHEL7 64bit 5 221
Percona cluster member randomly crash 15 338
updating the repos in yum 4 107
Linux Search 3 64
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 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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now