Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread-specific singleton (from @cypof) #2067

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions include/caffe/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ private:\
// See PR #1236
namespace cv { class Mat; }

// Avoid issues with including boost/thread in NVCC source (#1009).
namespace boost { template <typename T> class thread_specific_ptr; }

namespace caffe {

// We will use the boost shared_ptr instead of the new C++11 one mainly
Expand Down Expand Up @@ -98,12 +101,7 @@ void GlobalInit(int* pargc, char*** pargv);
class Caffe {
public:
~Caffe();
inline static Caffe& Get() {
if (!singleton_.get()) {
singleton_.reset(new Caffe());
}
return *singleton_;
}
static Caffe& Get();
enum Brew { CPU, GPU };

// This random number generator facade hides boost and CUDA rng
Expand Down Expand Up @@ -158,7 +156,7 @@ class Caffe {
shared_ptr<RNG> random_generator_;

Brew mode_;
static shared_ptr<Caffe> singleton_;
static boost::thread_specific_ptr<Caffe> singleton_;

private:
// The private constructor to avoid duplicate instantiation.
Expand Down
10 changes: 9 additions & 1 deletion src/caffe/common.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <boost/thread.hpp>
#include <glog/logging.h>
#include <cstdio>
#include <ctime>
Expand All @@ -7,7 +8,7 @@

namespace caffe {

shared_ptr<Caffe> Caffe::singleton_;
boost::thread_specific_ptr<Caffe> Caffe::singleton_;

// random seeding
int64_t cluster_seedgen(void) {
Expand Down Expand Up @@ -39,6 +40,13 @@ void GlobalInit(int* pargc, char*** pargv) {
::google::InstallFailureSignalHandler();
}

Caffe& Caffe::Get() {
if (!singleton_.get()) {
singleton_.reset(new Caffe());
}
return *singleton_;
}

#ifdef CPU_ONLY // CPU-only Caffe.

Caffe::Caffe()
Expand Down