mutex

Mutex analog in SQL?

戏子无情 提交于 2019-12-01 10:53:52
问题 I have multiple threads, executing similar queries. They shouldn't be executed the same time. I can create and check mutex for this purpose from ADO.Net client, but for some reason I'd prefer to do this using SQL. Is it possible? Regards, 回答1: Yes it is. Application Locks (or Mutexes) in SQL Server 2005 回答2: Use the locking available in SQL Server: Introduction to Locking in SQL Server 来源: https://stackoverflow.com/questions/4596377/mutex-analog-in-sql

Compiler says that data cannot be shared between threads safely even though the data is wrapped within a Mutex

社会主义新天地 提交于 2019-12-01 08:55:10
I'm using Rocket which has a State that it passes to the HTTP requests. This struct contains a Mutex<DatastoreInstance> which gives access to a SQLite database and is locked with a mutex to make read and writes safe. pub struct DatastoreInstance { conn: Connection, } When the DatastoreInstance struct looked like this, with only a SQLite connection everything worked fine, but I then also wanted to add a transaction object within this struct: pub struct DatastoreInstance { conn: Connection, events_transaction: Transaction, } This did not compile because the Transaction object needs to reference

Can I implement blocking queue using Semaphore in Java?

蹲街弑〆低调 提交于 2019-12-01 08:50:36
问题 I wonder if it is possible to use Semaphore to implement blocking queue? In the below codes, I use one Semaphore to protect the critical section, and two more Semaphore objects to track the number of empty slots and filled objects. public class BlockingQueue { private List<Object> queue = new LinkedList<Object>(); private int limit; private Semaphore slots; // semaphore for empty slots private Semaphore objs; // semaphore for filled slots private Semaphore mutex; // for the critical section

Can I read a bool variable in a thread without mutex? [duplicate]

狂风中的少年 提交于 2019-12-01 08:30:28
问题 This question already has answers here : In a multi-threaded C++ app, do I need a mutex to protect a simple boolean? (5 answers) Closed last year . Is there anything wrong in following source code if I don't use mutex? bool bStop = false; void thread1_fun() { while (!bStop) { doSomething(); } } void thread2_fun() { bStop = true; } 回答1: It is undefined behaviour to write to an object in one thread while another thread accesses the object at all. Unless you specifically inform the compiler that

Why is std::mutex taking a long, highly irregular amount of time to be shared?

扶醉桌前 提交于 2019-12-01 08:23:44
This code demonstrates that the mutex is being shared between two threads, but one thread has it nearly all of the time. #include <thread> #include <mutex> #include <iostream> #include <unistd.h> int main () { std::mutex m; std::thread t ([&] () { while (true) { { std::lock_guard <std::mutex> thread_lock (m); sleep (1); // or whatever } std::cerr << "#"; std::cerr.flush (); } }); while (true) { std::lock_guard <std::mutex> main_lock (m); std::cerr << "."; std::cerr.flush (); } } Compiled with g++ 7.3.0 on Ubuntu 18.04 4.15.0-23-generic. The output is a mix of both # and . characters, showing

Why would WaitForSingleObject return WAIT_FAILED

耗尽温柔 提交于 2019-12-01 07:00:33
MSDN says If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError. The code is: HANDLE m_mutex_handle; /**< m_mutex_handle. The handle to the created mutex. */ m_mutex_handle = ::CreateMutex( 0, false, NULL ); ::WaitForSingleObject( m_mutex_handle, INFINITE ); But what are the reasons that could happen? If you lack the SYNCHRONIZE privilege on the object, then you cannot wait. WAIT_FAILED will be returned. Passing in a bogus object might cause that. Closing a handle while the handle is being waited on can also cause undefined behaviour. I

cygwin pthread_mutex_timedlock surrogate

社会主义新天地 提交于 2019-12-01 06:51:48
Unfortunately the cygwin GCC 4.5.3 pthread library implementation doesn't support the POSIX standard function int pthread_mutex_timedlock(pthread_mutex_t* mutex, struct timespec* abstime); Has anyone a good idea how to implement a good workaround for this method in a mutex wrapper class? May be using pthread_mutex_trylock() with a (milliseconds based) nanosleep() call? I don't have a good feeling about the latter idea, but anyway the C++ implementation could look like this: bool MyPosixMutexWrapper::try_lock(const TimeDuration<>& timeout) { if(valid) { if(timeout == TimeDuration<>::Zero) { if

Unexpected behavior using std::try_to_lock

一个人想着一个人 提交于 2019-12-01 06:47:11
I get surprising and conflicting behavior when I try to run the following code. #include <iostream> #include <mutex> int main() { std::mutex mtx; std::unique_lock<std::mutex> lock1(mtx); std::unique_lock<std::mutex> lock2(mtx, std::try_to_lock); std::cout << "lock1 owns lock: " << lock1.owns_lock() << std::endl; std::cout << "lock2 owns lock: " << lock2.owns_lock() << std::endl; } When I run this on my computer (linux with either clang++ 4.0.1 or g++ 7.3.0) it prints out that both lock1 and lock2 own the lock (surprising). When I run this on cpp.sh it says that lock1 does own, but lock2 does

Single instance windows forms application and how to get reference on it?

吃可爱长大的小学妹 提交于 2019-12-01 06:42:58
I have a Windows Forms application that allows only one instance to be running at the time. I have implemented Singleton by using Mutex. The Application must be startable from commandline (with or without parameters). Application is started and exited by script. User can not take any action on it. So, application purpose is simple "indicator" application that will just display some visual and graphical information for the enduser. End user can not do anything with it, just see it. It is windows forms application because then visual and graphical appearance is relatively easy implement (you can

c++11 std::mutex compiler error in Visual Studio 2012

懵懂的女人 提交于 2019-12-01 06:13:51
问题 This a quest about deadlock in C++11 standard. In the sec3.2.4 of C++ Concurrency in Action, there is an example for preventing multithreads from deadlock. For guys without this book, in addition, there is an another almost similar example you can refer to: http://en.cppreference.com/w/cpp/thread/lock_tag The problem I encountered is that the codes of both codes arise compiler-errors in Visual Studio 2012. The error message is: 'std::mutex::mutex': cannot access private member declared in