Solved

How to suspend/resume a Posix thread

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

20 Experts available now in Live!

Get 1:1 Help Now