linux线程的挂起和唤醒
2017-09-05 15:49
225 查看
#include <iostream> #include <unistd.h> #include <stdlib.h> #include <stdio.h> using namespace std; pthread_t IDA; pthread_t IDB; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; #define RUN 1 #define STOP 0 int status = RUN; void * thread_function(void *param) { static int i = 0; while (1) { pthread_mutex_lock(&mut); while (!status) { pthread_cond_wait(&cond, &mut); } pthread_mutex_unlock(&mut); //do actual something printf("thread is running...\n"); 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); } } void thread_pause() { if (status == RUN) { pthread_mutex_lock(&mut); status = STOP; printf("thread stop!\n"); pthread_mutex_unlock(&mut); } } void *test(void *param) { while(1) { sleep(5); thread_pause(); sleep(10); thread_resume(); sleep(5); } } int main(int argc, char *argv[]) { pthread_create(&IDA, NULL, test, NULL); pthread_create(&IDB, NULL, thread_function, NULL); pthread_join(IDA, NULL); pthread_join(IDB, NULL); return 0; }
相关文章推荐
- Linux 线程挂起与唤醒功能 实例
- 根据Linux 线程挂起与唤醒原理,实现Sleep的暂停与继续
- java线程技术6_线程的挂起和唤醒
- VC线程的挂起及唤醒
- C++ 线程的创建,挂起,唤醒,终止
- C++多线程编程——线程的挂起、唤醒与终止
- java线程技术6_线程的挂起和唤醒
- Linux下线程的挂起和恢复 http://blog.chinaunix.net/uid-21765995-id-1815655.html
- poll and epoll的区别-linux C线程间通信:阻塞-唤醒-创建文件,重启后大小是0
- Linux 2. 关于pthread_cond_signal 唤醒两个线程的问题
- C++ 线程的创建,挂起,唤醒,终止
- 线程的挂起与唤醒之实现app加载广告点击跳转功能
- linux 线程挂起恢复
- Java 多线程使用:线程的挂起与重新唤醒
- [置顶] C# 线程的挂起与唤醒 (AutoResetEvent,ManualResetEvent)
- C++多线程编程――线程的挂起、唤醒与终止
- Linux中对线程的挂起与恢复的实现
- Linux下线程的挂起和恢复
- 线程的挂起与唤醒之实现app加载广告点击跳转功能
- Linux下线程的挂起和恢复