Pthead thread pause and resume discussion, a very good answer http://bbs.csdn.net/topics/390321574
I hope to have a great help to myself and the friends who need it ....
Refer to that, select and modify the part of the brother's code, and now you can achieve that function
void Pthread_suspend (void) {if (Pthread_pause = = False) {Pthread_mutex_lock (&mutex_pause);
Pthread_pause = true;
printf ("\ n------Transport pause------\ n");
Pthread_mutex_unlock (&mutex_pause);
else {printf ("The Transport suspend already/n");
} void Pthread_resume (void) {if (Pthread_pause = = True) {Pthread_mutex_lock (&mutex_pause);
Pthread_pause = false;
Pthread_cond_broadcast (&cond_pause);
printf ("------Transport resume------/n");
Pthread_mutex_unlock (&mutex_pause);
else {printf ("Transport resume already/n"); } void* ThreadFunc (void* args)//worker thread function {while (1) {if (!pthread_pause) {printf (
"Working ...");
Work ();
else {printf ("Rest ...");
Sleep (1);
Return ((void*) 0); } void* ThreadFunc (void* args)//worker thread function {while (1) {
if (Pthread_pause = = False) {work ();
else {pthread_cond_wait (&cond_pause,&mutex_pause);
} return ((void*) 0);
int main () {pthread_t id;
int ret;
ret = Pthread_create (&id,null,threadfunc, (void *) sockclient);
if (ret!= 0) {printf ("Create thread error!\r\n");
Exit (1);
} pthread_create (&thread, NULL, THREADFUNC, NULL);
while (1) {if (Kbhit ())//has keyboard input Kbhit! 0;
{ch = getch ();
if (ch = = ' s ') {pthread_suspend ();
} if (ch = = ' R ') {Pthread_resume ();
}} return 0; }