pthead线程的暂停与恢复的讨论,一个非常好的回答 http://bbs.csdn.net/topics/390321574
希望对自己和有需要的朋友有很大的帮助。。。。
自己参考那个,选择和修改了那位兄弟的部分代码 ,,现在可以实现那个功能
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) //工作线程函数
{
while(1)
{
if(!pthread_pause)
{
printf("working...");
//work();
}
else
{
printf("rest...");
}
sleep(1);
}
return ((void*)0);
}
void* threadFunc(void* args) //工作线程函数
{
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() )//有键盘输入 kbhit !0;
{
ch = getch();
if( ch == 's' )
{
pthread_suspend();
}
if(ch == 'r')
{
pthread_resume();
}
}
}
return 0;
}