Skip to content

Commit 3c147c7

Browse files
committed
fix proc num
1 parent b799067 commit 3c147c7

File tree

7 files changed

+50
-19
lines changed

7 files changed

+50
-19
lines changed

build.sh renamed to build_debug.sh

File renamed without changes.

burger/base/tests/Atomic_unittest.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "burger/base/Atomic.h"
22
#include <cassert>
3+
#include <iostream>
34

45
int main()
56
{
@@ -34,4 +35,18 @@ int main()
3435
assert(a1.getAndSet(100) == 2);
3536
assert(a1.get() == 100);
3637
}
38+
{
39+
burger::AtomicInt32 a2;
40+
if(a2.get()) {
41+
std::cout << "a2 is true" << std::endl;
42+
} else {
43+
std::cout << "a2 is false" << std::endl;
44+
}
45+
a2.increment();
46+
if(a2.get()) {
47+
std::cout << "a2 increment is true" << std::endl;
48+
} else {
49+
std::cout << "a2 increment is false" << std::endl;
50+
}
51+
}
3752
}

burger/base/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# add_executable(timestamp_unittest Timestamp_unittest.cc)
33
# target_link_libraries(timestamp_unittest burger_base)
44

5-
# add_executable(atomic_unittest Atomic_unittest.cc)
5+
add_executable(atomic_unittest Atomic_unittest.cc)
66

77
# add_executable(exception_unittest Exception_unittest.cc)
88
# target_link_libraries(exception_unittest burger_base)

burger/net/CoTcpServer.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ CoTcpServer::~CoTcpServer() {
4444

4545
void CoTcpServer::setThreadNum(size_t threadNum) {
4646
assert(threadNum > 0);
47-
sched_->setThreadNum(threadNum);
47+
sched_->setWorkProcNum(threadNum);
4848
}
4949

5050
// 多次调用无害
@@ -61,7 +61,7 @@ void CoTcpServer::setConnectionHandler(const ConnectionHandler& handler) {
6161
}
6262

6363
void CoTcpServer::startAccept() {
64-
while(true) {
64+
while(started_.get()) {
6565
InetAddress peerAddr;
6666
int connfd = listenSock_->accept(peerAddr);
6767
if(connfd > 0) {
@@ -75,10 +75,11 @@ void CoTcpServer::startAccept() {
7575
// 此处跨线程调用
7676
proc->addTask(std::bind(connHandler_, conn), "connHandler");
7777
} else {
78-
ERROR("in Acceptor::handleRead");
78+
ERROR("accept error");
7979
// 当fd达到上限,先占住一个空的fd,然后当fd满了,就用这个接受然后关闭
8080
// 这样避免一直水平电平一直触发,先去读走他
8181
if(errno == EMFILE) {
82+
ERROR("Exceed the maximum fd nums");
8283
sockets::close(idleFd_);
8384
idleFd_ = ::accept(listenSock_->getFd(), nullptr, nullptr);
8485
sockets::close(idleFd_);

burger/net/Scheduler.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ Scheduler::~Scheduler() {
3636
}
3737

3838
// before start to call
39-
void Scheduler::setThreadNum(size_t threadNum) {
40-
threadNum_ = threadNum;
39+
void Scheduler::setWorkProcNum(size_t workProcNum) {
40+
if(workProcNum > 100) {
41+
ERROR("set too many processors");
42+
workProcNum_ = 0;
43+
}
44+
workProcNum_ = workProcNum;
4145
}
4246

4347
void Scheduler::start() {
@@ -46,10 +50,10 @@ void Scheduler::start() {
4650
std::unique_ptr<Processor> mainProc = util::make_unique<Processor>(this);
4751
mainProc_ = mainProc.get();
4852

49-
for(size_t i = 0; i < threadNum_ - 1; i++) {
50-
auto procThrd = std::make_shared<ProcessThread>(this);
51-
workThreadVec_.push_back(procThrd);
53+
for(size_t i = 0; i < workProcNum_; i++) {
54+
auto procThrd = util::make_unique<ProcessThread>(this);
5255
workProcVec_.push_back(procThrd->startProcess());
56+
workThreadVec_.push_back(std::move(procThrd));
5357
}
5458
running_ = true;
5559
cv_.notify_one(); // todo : 无其他线程,有影响吗
@@ -148,7 +152,7 @@ void Scheduler::cancel(TimerId timerId) {
148152

149153
size_t Scheduler::getWorkProcNum() {
150154
if(workProcVec_.empty()) return 1;
151-
return threadNum_ - 1;
155+
return workProcNum_;
152156
}
153157

154158
// 如果只有一个线程,那么返回mainProc, 否则返回工作Proc
@@ -171,7 +175,7 @@ Processor* Scheduler::pickOneWorkProcessor() {
171175

172176
void Scheduler::joinThread() {
173177
if(thread_.joinable()) thread_.join();
174-
for(auto thrd : workThreadVec_) {
178+
for(auto& thrd : workThreadVec_) {
175179
thrd->join();
176180
}
177181
quit_ = true;

burger/net/Scheduler.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Scheduler : boost::noncopyable {
2828
Scheduler();
2929
~Scheduler();
3030

31-
void setThreadNum(size_t threadNum);
31+
void setWorkProcNum(size_t workProcNum);
3232
void startAsync();
3333
void wait();
3434
void stop();
@@ -46,26 +46,22 @@ class Scheduler : boost::noncopyable {
4646
TimerId runAfter(double delay, TimerCallback cb, const std::string& name = "timer");
4747
TimerId runEvery(double interval, TimerCallback cb, const std::string& name = "timer");
4848
void cancel(TimerId timerId);
49-
50-
// protected:
51-
49+
5250
Processor* getMainProcessor() { return mainProc_; }
5351
std::vector<Processor *> getWorkProcList() { return workProcVec_; }
5452
size_t getWorkProcNum();
5553
Processor* pickOneWorkProcessor();
56-
57-
size_t getThreadNum() { return threadNum_; }
5854

5955
private:
6056
void start();
6157
void joinThread();
6258
private:
6359
bool running_ = false;
6460
bool quit_ = false;
65-
size_t threadNum_ = 1;
61+
size_t workProcNum_ = 0;
6662
Processor* mainProc_;
6763
std::vector<Processor *> workProcVec_; // todo : 优先队列
68-
std::vector<std::shared_ptr<ProcessThread> > workThreadVec_;
64+
std::vector<std::unique_ptr<ProcessThread> > workThreadVec_;
6965
std::thread thread_;
7066
std::mutex mutex_;
7167
std::condition_variable cv_;

tests/test_vector_unique_ptr.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <vector>
2+
#include <memory>
3+
#include <iostream>
4+
5+
6+
// g++ std=c++14
7+
int main() {
8+
std::vector<std::unique_ptr<int> > vec;
9+
vec.push_back(std::make_unique<int>(1));
10+
vec.push_back(std::make_unique<int>(2));
11+
vec.push_back(std::make_unique<int>(3));
12+
for(auto& v : vec) {
13+
std::cout << *v << std::endl;
14+
}
15+
}

0 commit comments

Comments
 (0)