15 Star 25 Fork 3

eclipser/thread

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.cpp 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
Administrator 提交于 2014-01-17 17:31 . 事例5
/*
* File: main.cpp
* Author: Vicky.H
*/
#include <iostream>
#include <time.h>
#include "Thread.h"
#include "Lock.h"
class MyThread : public Thread {
public:
void run() {
for(;;) {
sleep(1);
std::cout << "thread 1" << time(0) << std::endl;
}
}
};
class MyThread2 : public Thread {
public:
void run() {
for(;;) {
sleep(5);
std::cout << "thread 2" << time(0) << std::endl;
}
}
};
class MyThread3 : public Thread {
public:
void run() {
std::cout << "thread 3" << time(0) << std::endl;
}
};
class MyThread4 : public Thread {
public:
MyThread4() : isAlive (false) {
}
void start() {
lock.Lock();
if (!isAlive)
isAlive = true;
lock.Unlock();
Thread::start();
}
void stop() {
lock.Lock();
if (isAlive)
isAlive = false;
lock.Unlock();
}
void run() {
bool _isAlive;
lock.Lock();
_isAlive = isAlive;
lock.Unlock();
while(_isAlive) {
sleep(1);
std::cout << "thread 4" << time(0) << std::endl;
lock.Lock();
_isAlive = isAlive;
lock.Unlock();
}
}
private:
bool isAlive;
MyLock lock;
};
/*
*
*/
int main(void) {
// MyThread myThread;
// myThread.start();
//
// sleep(2);
//
// std::cout << "main thread!" << std::endl;
// std::cout << g_QuitThreadCount << std::endl;
//
// sleep(2);
//
// MyThread2 myThread2;
// myThread2.start();
//
// sleep(1);
// std::cout << "main thread!" << std::endl;
// std::cout << g_QuitThreadCount << std::endl;
//
// MyThread3 myThread3;
// myThread3.start();
//
// sleep(1);
// std::cout << g_QuitThreadCount << std::endl;
MyThread4 myThread4;
myThread4.start();
sleep(10);
std::cout << "myThread4 stop !" << std::endl;
myThread4.stop();
// 保持父线程运行
sleep(100);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/eclipser/thread.git
[email protected]:eclipser/thread.git
eclipser
thread
thread
master

搜索帮助