Discussion of pauses and restores for pthread threads

Source: Internet
Author: User

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; }




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.