Permalink
Browse files

use atomic variable to fix TcpServer::start thread-safety issue. fix …

…issue #25
  • Loading branch information...
1 parent f14fb3a commit f9e094dfada0ef4f7a766eab549d5d0a64f5bd97 @donzell donzell committed with Oct 3, 2013
Showing with 4 additions and 7 deletions.
  1. +2 −6 muduo/net/TcpServer.cc
  2. +2 −1 muduo/net/TcpServer.h
@@ -32,7 +32,6 @@ TcpServer::TcpServer(EventLoop* loop,
threadPool_(new EventLoopThreadPool(loop)),
connectionCallback_(defaultConnectionCallback),
messageCallback_(defaultMessageCallback),
- started_(false),
nextConnId_(1)
{
acceptor_->setNewConnectionCallback(
@@ -63,14 +62,11 @@ void TcpServer::setThreadNum(int numThreads)
void TcpServer::start()
{
- if (!started_)
+ if (started_.getAndSet(1) == 0)
{
- started_ = true;
threadPool_->start(threadInitCallback_);
- }
- if (!acceptor_->listenning())
- {
+ assert(!acceptor_->listenning());
loop_->runInLoop(
boost::bind(&Acceptor::listen, get_pointer(acceptor_)));
}
@@ -11,6 +11,7 @@
#ifndef MUDUO_NET_TCPSERVER_H
#define MUDUO_NET_TCPSERVER_H
+#include <muduo/base/Atomic.h>
#include <muduo/base/Types.h>
#include <muduo/net/TcpConnection.h>
@@ -106,7 +107,7 @@ class TcpServer : boost::noncopyable
MessageCallback messageCallback_;
WriteCompleteCallback writeCompleteCallback_;
ThreadInitCallback threadInitCallback_;
- bool started_;
+ AtomicInt32 started_;
// always in loop thread
int nextConnId_;
ConnectionMap connections_;

0 comments on commit f9e094d

Please sign in to comment.