mutex

graceful thread termination with pthread_cond_signal proving problematic

半世苍凉 提交于 2020-01-04 05:18:22
问题 I need to fire of a bunch of threads and would like to bring them down gracefully. I'm trying to use pthread_cond_signal / pthread_cond_wait to achieve this but am running into a problem. Here's my code. firstly the thread_main static void *thrmain( void * arg ) { // acquire references to the cond var, mutex, finished flag and // message queue ..... while( true ) { pthread_mutex_lock( &lock ); if ( msq.empty() ) { // no messages so wait for one. pthread_cond_wait( &cnd, &lock ); } // are we

graceful thread termination with pthread_cond_signal proving problematic

旧街凉风 提交于 2020-01-04 05:18:06
问题 I need to fire of a bunch of threads and would like to bring them down gracefully. I'm trying to use pthread_cond_signal / pthread_cond_wait to achieve this but am running into a problem. Here's my code. firstly the thread_main static void *thrmain( void * arg ) { // acquire references to the cond var, mutex, finished flag and // message queue ..... while( true ) { pthread_mutex_lock( &lock ); if ( msq.empty() ) { // no messages so wait for one. pthread_cond_wait( &cnd, &lock ); } // are we

boost::shared_ptr boost::mutex and copy constructor

送分小仙女□ 提交于 2020-01-04 03:48:50
问题 I need to protect the access to a data structure in my class. As I can't have mutex (because I can't copy it) I am considering to have shared_ptr and keep the mutex there. Here is a sample code of my idea: class Sample { typedef boost::lock_guard<boost::mutex> AcquireLock; boost::shared_ptr<boost::mutex> mutt; public: Sample() : mutt(new boost::mutex) {} void Method() { AcquireLock lock(*mutt); //do some work here } }; I've got the following questions: Is it a bad practice to use the mutex

pthread mutex locking variables used in statements

[亡魂溺海] 提交于 2020-01-03 20:16:53
问题 First of all, I have a gut feeling that says, inside an if statement, if I am using the variable, it counts as reading the variable so I should lock it with mutex there also (if another pthread might be doing stuff with it). Am I right that I should lock it? The example situation in simplified manner is given below. in one thread I am using the below statement: if(event){ // Should I or should I not lock event here to use it // inside if statement? pthread_mutex_lock(&mutex_event); event = 0;

Mutex across network - C#.NET

泪湿孤枕 提交于 2020-01-03 18:42:08
问题 I have a message queue on a shared system. This queue is accessed by 2 processes which run on 2 other machines. I need to control access to this queue by the 2 processes. So I need a "network mutex". How can I achieve this? I don't think this is supported out of the box in C#.NET but if I have missed something very obvious, do point me in the right direction. This question has been asked before but the solutions suggested involved a database. I dont have any database in question. How To Mutex

How to lock a Rust struct the way a struct is locked in Go?

前提是你 提交于 2020-01-03 07:17:46
问题 I'm trying to learn how to do locks in Rust the way they work in Go. With Go I can do something like: type Info struct { sync.RWMutex height uint64 verify bool } If I have some function/method acting on info I can do this: func (i *Info) DoStuff(myType Data) error { i.Lock() //do my stuff } It seems like what I need is the sync.RWMutex, so this is what I have tried: pub struct Info { pub lock: sync.RWMutex, pub height: u64, pub verify: bool, } Is this the correct approach? How would I proceed

Golang, how to share value - message or mutex?

ε祈祈猫儿з 提交于 2020-01-03 04:54:12
问题 I've done simple bench mark which one is more efficient among message-passing and locking for shared value. Firstly, please check code bellow. package main import ( "flag" "fmt" "math/rand" "runtime" "sync" "time" ) type Request struct { Id int ResChan chan Response } type Response struct { Id int Value int } func main() { procNum := flag.Int("proc", 1, "Number of processes to use") clientNum := flag.Int("client", 1, "Number of clients") mode := flag.String("mode", "message", "message or

In Go, can we synchronize each key of a map using a lock per key?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 04:10:29
问题 In Go, can we synchronize each key of a map using a lock per key? Is map level global lock always required? The documentation says that any access to map is not thread safe. But if a key exists, then can it be locked individually? 回答1: Not exactly, but if you are only reading pointers off a map and modifying the referents, then you aren't modifying the map itself. 回答2: This is a simple implementation of what you want: mapmutex. Basically, a mutex is used to guard the map and each item in the

In Go, can we synchronize each key of a map using a lock per key?

家住魔仙堡 提交于 2020-01-03 04:10:24
问题 In Go, can we synchronize each key of a map using a lock per key? Is map level global lock always required? The documentation says that any access to map is not thread safe. But if a key exists, then can it be locked individually? 回答1: Not exactly, but if you are only reading pointers off a map and modifying the referents, then you aren't modifying the map itself. 回答2: This is a simple implementation of what you want: mapmutex. Basically, a mutex is used to guard the map and each item in the

Close handle to a mutex in another process

╄→гoц情女王★ 提交于 2020-01-03 03:15:07
问题 I want to close a handle to a mutex located in another process, so I can run more than one instance of the application. I already know this can be done, see Process Explorer. Example: Windows Minesweeper (Windows 7) uses a mutex to only allow one game, so I thought I would use it as an example since it's pre-installed with Windows and therefore easier for you guys to guide me. The mutex that I need to close is \Sessions\1\BaseNamedObjects\Oberon_Minesweeper_Singleton , which I found using