thread-safety

C++ Meyers Singleton - thread safe (code equivalent for mutex?)

风流意气都作罢 提交于 2020-01-17 05:08:06
问题 I was pointed last week about a piece of code like this: #include <pthread.h> namespace NSTest { class SingletonClass { public: static SingletonClass & getInstance() { static pthread_mutex_t mutex; pthread_mutex_lock(&mutex); if(singletonPtr==nullptr) { createInstence(); } return (*singletonPtr); pthread_mutex_unlock(&mutex); } private: static void createInstence() { static SingletonClass static_SingletonClass; singletonPtr=&static_SingletonClass; } static SingletonClass * singletonPtr; };

Rust - Pass a function reference to threads

断了今生、忘了曾经 提交于 2020-01-16 19:35:48
问题 Say I have a struct like: pub struct MyStruct { f: Arc<dyn Fn(Vec<f64>) -> Vec<f64>>, } impl MyStruct { pub fn new(f: Arc<dyn Fn(Vec<f64>) -> Vec<f64>>) -> MyStruct { MyStruct { f } } pub fn start(&self) { for _ in 0..5 { let f = self.f.clone(); thread::spawn(move || { let v: Vec<f64> = get_random_vector(); let v = (f)(v); // do something with v }); } } } I'm getting an error that the function cannot be shared between threads safely since the dyn Fn(Vec<f64>) -> Vec<f64>) type doesn't

What is a thread-safe random number generator for perl?

跟風遠走 提交于 2020-01-16 11:50:27
问题 The core perl function rand() is not thread-safe, and I need random numbers in a threaded monte carlo simulation. I'm having trouble finding any notes in CPAN on the various random-number generators there as to which (if any) are thread-safe, and every google search I do keeps getting cluttered with C/C++/python/anything but perl. Any suggestions? 回答1: Do not use built-in rand for Monte Carlo on Windows. At least, try: my %r = map { rand() => undef } 1 .. 1_000_000; print scalar keys %r, "\n"

What is a thread-safe random number generator for perl?

£可爱£侵袭症+ 提交于 2020-01-16 11:50:11
问题 The core perl function rand() is not thread-safe, and I need random numbers in a threaded monte carlo simulation. I'm having trouble finding any notes in CPAN on the various random-number generators there as to which (if any) are thread-safe, and every google search I do keeps getting cluttered with C/C++/python/anything but perl. Any suggestions? 回答1: Do not use built-in rand for Monte Carlo on Windows. At least, try: my %r = map { rand() => undef } 1 .. 1_000_000; print scalar keys %r, "\n"

ThreadPoolExecutor - ArrayBlockingQueue … to wait before it removes an element form the Queue

假装没事ソ 提交于 2020-01-16 03:25:25
问题 I am trying to Tune a thread which does the following: A thread pool with just 1 thread [CorePoolSize =0, maxPoolSize = 1] The Queue used is a ArrayBlockingQueue Quesize = 20 BackGround: The thread tries to read a request and perform an operation on it. HOWEVER, eventually the requests have increased so much that the thread is always busy and consume 1 CPU which makes it a resource hog. What I want to do it , instead sample the requests at intervals and process them . Other requests can be

Double-checked locking around value going to null?

廉价感情. 提交于 2020-01-16 00:54:14
问题 Is this thread safe: class X; class Once { public: Once(X* x) : x_(x) {} X* Get() { if (!x_) return NULL; // all dirty reads end up here. // This could be any type of scoped lock... some_scoped_lock lock(m); // if (!x_) return x_; // omitted because it's a no op X* ret(x_); // might get NULL if we waited for lock x_ = NULL; // idempotent return ret; } private: X *x_; some_kind_of_mutex m; // Boilerplate to make all other constructors and default function private .... } ( edit: i'm interested

popen - locks or not thread safe?

寵の児 提交于 2020-01-15 06:30:34
问题 I've seen a few implementations of popen()/pclose(). They all used a static list of pids, and no locking: static int *pids; static int fds; if (!pids) { if ((fds = getdtablesize()) <= 0) return (NULL); if ((pids = malloc(fds * sizeof(int))) == NULL) return (NULL); memset(pids, 0, fds * sizeof(int)); } Or this, supposedly NetBSD: static struct pid { struct pid *next; FILE *fp; pid_t pid; } *pidlist; /* Link into list of file descriptors. */ cur->fp = iop; cur->pid = pid; cur->next = pidlist;

popen - locks or not thread safe?

为君一笑 提交于 2020-01-15 06:30:26
问题 I've seen a few implementations of popen()/pclose(). They all used a static list of pids, and no locking: static int *pids; static int fds; if (!pids) { if ((fds = getdtablesize()) <= 0) return (NULL); if ((pids = malloc(fds * sizeof(int))) == NULL) return (NULL); memset(pids, 0, fds * sizeof(int)); } Or this, supposedly NetBSD: static struct pid { struct pid *next; FILE *fp; pid_t pid; } *pidlist; /* Link into list of file descriptors. */ cur->fp = iop; cur->pid = pid; cur->next = pidlist;

Is Task.ContinueWith thread safe?

非 Y 不嫁゛ 提交于 2020-01-15 06:18:42
问题 I have multiple threads that enqueue actions to the same task using ContinueWith() Is it thread safe? Or should I wrap it with some dedicated lock? 回答1: It is thread safe. It even works when the task has already completed. 来源: https://stackoverflow.com/questions/31165280/is-task-continuewith-thread-safe

Is Task.ContinueWith thread safe?

坚强是说给别人听的谎言 提交于 2020-01-15 06:17:57
问题 I have multiple threads that enqueue actions to the same task using ContinueWith() Is it thread safe? Or should I wrap it with some dedicated lock? 回答1: It is thread safe. It even works when the task has already completed. 来源: https://stackoverflow.com/questions/31165280/is-task-continuewith-thread-safe