linux中线程的挂起与恢复(进程暂停)
生活随笔
收集整理的這篇文章主要介紹了
linux中线程的挂起与恢复(进程暂停)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
今
天
在網(wǎng)上查了一下
linux中對(duì)進(jìn)程的掛起與恢復(fù)的實(shí)現(xiàn),相關(guān)資料少的可憐,大部分都是粘貼復(fù)制。也沒(méi)有完整詳細(xì)的代碼。故自己整理了一下
程序流程為:主線程創(chuàng)建子線程(當(dāng)前子線程狀態(tài)為stop停止?fàn)顟B(tài)),5秒后主線程喚醒子線程,10秒后主線程掛起子線程,15秒后主線程再次喚醒子線程,20秒后主線程執(zhí)行完畢等待子線程退出。
代碼如下: #include #include #include #include #include
#define RUN 1 #define STOP 0
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int status = STOP; void * thread_function(void) { ???static int i = 0; ???while (1)? ???{ ?? ???????pthread_mutex_lock(&mut); ???????while (!status) ???????{ ???????????pthread_cond_wait(&cond, &mut); ???????} ???????pthread_mutex_unlock(&mut); ??? ???????printf("child pthread %d\n", i++); ???????if (i == 20)? ???????????break; ???????sleep(1); ???} ?? }
void thread_resume() { ???if (status == STOP) ???{ ?? ???????pthread_mutex_lock(&mut); ???????status = RUN; ???????pthread_cond_signal(&cond); ???????printf("pthread run!\n"); ???????pthread_mutex_unlock(&mut); ???} ?? ???else ???{ ?? ???????printf("pthread run already\n"); ???} ?? }
void thread_pause() { ???if (status == RUN) ???{ ?? ???????pthread_mutex_lock(&mut); ???????status = STOP; ???????printf("thread stop!\n"); ???????pthread_mutex_unlock(&mut); ???} ?? ???else ???{ ?? ???????printf("pthread pause already\n"); ???} }
int main() { ???int err; ???static int i = 0; ???pthread_t child_thread;
#if 0 ???if (pthread_mutex_init(&mut, NULL) != 0) ???????printf("mutex init error\n"); ???if (pthread_cond_init(&cond, NULL) != 0) ???????printf("cond init error\n"); #endif
???err = pthread_create(&child_thread, NULL, (void *)thread_function, NULL); ???if (err != 0 ) ???????printf("can't create thread: %s\n", strerror(err)); ???while(1) ???{ ???????printf("father pthread %d\n", i++); ???????sleep(1); ???????if (i == 5) ???????????thread_resume(); ???????if (i == 10) ???????????thread_pause(); ???????if (i == 15) ???????????thread_resume(); ???????if (i == 20) ???????????break; ???} ???if (0 == pthread_join(child_thread, NULL)) ???????printf("child thread is over\n"); ???return 0; }
程序流程為:主線程創(chuàng)建子線程(當(dāng)前子線程狀態(tài)為stop停止?fàn)顟B(tài)),5秒后主線程喚醒子線程,10秒后主線程掛起子線程,15秒后主線程再次喚醒子線程,20秒后主線程執(zhí)行完畢等待子線程退出。
代碼如下: #include #include #include #include #include
#define RUN 1 #define STOP 0
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int status = STOP; void * thread_function(void) { ???static int i = 0; ???while (1)? ???{ ?? ???????pthread_mutex_lock(&mut); ???????while (!status) ???????{ ???????????pthread_cond_wait(&cond, &mut); ???????} ???????pthread_mutex_unlock(&mut); ??? ???????printf("child pthread %d\n", i++); ???????if (i == 20)? ???????????break; ???????sleep(1); ???} ?? }
void thread_resume() { ???if (status == STOP) ???{ ?? ???????pthread_mutex_lock(&mut); ???????status = RUN; ???????pthread_cond_signal(&cond); ???????printf("pthread run!\n"); ???????pthread_mutex_unlock(&mut); ???} ?? ???else ???{ ?? ???????printf("pthread run already\n"); ???} ?? }
void thread_pause() { ???if (status == RUN) ???{ ?? ???????pthread_mutex_lock(&mut); ???????status = STOP; ???????printf("thread stop!\n"); ???????pthread_mutex_unlock(&mut); ???} ?? ???else ???{ ?? ???????printf("pthread pause already\n"); ???} }
int main() { ???int err; ???static int i = 0; ???pthread_t child_thread;
#if 0 ???if (pthread_mutex_init(&mut, NULL) != 0) ???????printf("mutex init error\n"); ???if (pthread_cond_init(&cond, NULL) != 0) ???????printf("cond init error\n"); #endif
???err = pthread_create(&child_thread, NULL, (void *)thread_function, NULL); ???if (err != 0 ) ???????printf("can't create thread: %s\n", strerror(err)); ???while(1) ???{ ???????printf("father pthread %d\n", i++); ???????sleep(1); ???????if (i == 5) ???????????thread_resume(); ???????if (i == 10) ???????????thread_pause(); ???????if (i == 15) ???????????thread_resume(); ???????if (i == 20) ???????????break; ???} ???if (0 == pthread_join(child_thread, NULL)) ???????printf("child thread is over\n"); ???return 0; }
總結(jié)
以上是生活随笔為你收集整理的linux中线程的挂起与恢复(进程暂停)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 统信UOS V20专业版系统更新:终于支
- 下一篇: 抛弃祖传1.5L发动机 “国民神车”新款