locking

Do I need a lock when only a single thread writes to a shared variable?

对着背影说爱祢 提交于 2019-12-30 03:20:08
问题 I have 2 threads and a shared float global. One thread only writes to the variable while the other only reads from it, do I need to lock access to this variable? In other words: volatile float x; void reader_thread() { while (1) { // Grab mutex here? float local_x = x; // Release mutex? do_stuff_with_value(local_x); } } void writer_thread() { while (1) { float local_x = get_new_value_from_somewhere(); // Grab mutex here? x = local_x; // Release mutex? } } My main concern is that a load or

How do I implement “pessimistic locking” in an asp.net application?

折月煮酒 提交于 2019-12-30 02:26:32
问题 I would like some advice from anyone experienced with implementing something like "pessimistic locking" in an asp.net application. This is the behavior I'm looking for: User A opens order #313 User B attempts to open order #313 but is told that User A has had the order opened exclusively for X minutes. Since I haven't implemented this functionality before, I have a few design questions: What data should i attach to the order record? I'm considering: LockOwnedBy LockAcquiredTime

How can I lock an InnoDB table to prevent updates while that table is being copied?

我是研究僧i 提交于 2019-12-30 01:31:14
问题 I would like to temporarily lock a table to prevent other concurrent processes from making changes to it. The reason for this is that this table is going to be copied to a temp table, altered, and then copied back (well original is actually dropped and new table is renamed). Then after all this is complete, I want to unlock the table and hopefully have anything that was attempted during the lock resume. I also need to be able to read from the table that has been locked to build the new table.

“The total number of locks exceeds the lock table size” Deleting 267 Records

帅比萌擦擦* 提交于 2019-12-29 07:34:09
问题 I'm trying to delete 267 records out of about 40 million. The query looks like: delete from pricedata where pricedate > '20120413' pricedate is a char(8) field. I know about adjusting innodb_buffer_pool_size , but if I can do select from pricedata where pricedate > '20120413' and get 267 records (and that's all there are), no errors, why does it choke on the delete? And if adjusting innodb_buffer_pool_size doesn't work, what should I do? 回答1: It seems that you don't have an index on pricedate

A tested implementation of Peterson Lock algorithm?

痴心易碎 提交于 2019-12-29 07:00:06
问题 Does anyone know of a good/correct implementation of Peterson's Lock algorithm in C? I can't seem to find this. Thanks. 回答1: I won't make any assertions about how good or correct the implementation is, but it was tested (briefly). This is a straight translation of the algorithm described on wikipedia. struct petersonslock_t { volatile unsigned flag[2]; volatile unsigned turn; }; typedef struct petersonslock_t petersonslock_t; petersonslock_t petersonslock () { petersonslock_t l = { { 0U, 0U }

How to find out what table a page lock belongs to

爱⌒轻易说出口 提交于 2019-12-29 04:36:09
问题 I'm using the sys.dm_tran_locks view to check what areas of my database have locks when we are having performance problems. Using this view.... If the resource_type is database I can use the DB_NAME function to find out what database has the lock. If its an object I can normally join to sys.tables to check what table it is. However if the resource_type is Page or Key is there any way to trace this back to its parent table so I can get a good idea of which tables are locking? 回答1: This is what

boost::unique_lock vs boost::lock_guard

吃可爱长大的小学妹 提交于 2019-12-29 02:33:09
问题 I don't well understand the difference betweeen these two lock classes. In boost documentation it is said, boost::unique_lock doesn't realize lock automatically. Does it mean that the main difference between unique_lock and lock_guard is that with unique_lock we must call explicitly the lock() function ? 回答1: First to answer your question. No you don't need to call lock on a unique_lock. See below: The unique_lock is only a lock class with more features. In most cases the lock_guard will do

Is there a `shared_lock_guard` and if not, what would it look like?

故事扮演 提交于 2019-12-29 01:43:11
问题 I wanted to use a std::mutex in my class, and noticed that it isn't copyable. I'm at the bottom level of my library here, so it seems like a terrible idea to have this behaviour. I used std::lock_guard on the std::mutex , but there doesn't seem to be a shared_lock_guard , which would be preferable to provide write-locks-exclusively behaviour. Is this an oversight or trivial to implement myself? 回答1: With C++14 You can use a std::shared_lock and a std::unique_lock to implement read/write

mysql insert race condition

痴心易碎 提交于 2019-12-28 12:34:47
问题 How do you stop race conditions in MySQL? the problem at hand is caused by a simple algorithm: select a row from table if it doesn't exist, insert it and then either you get a duplicate row, or if you prevent it via unique/primary keys, an error. Now normally I'd think transactions help here, but because the row doesn't exist, the transaction don't actually help (or am I missing something?). LOCK TABLE sounds like an overkill, especially if the table is updated multiple times per second. The

Windows 2008 R2 - Kernel (System Process PID=4) is locking files and folders

这一生的挚爱 提交于 2019-12-28 03:35:10
问题 Windows 2008 R2 - Kernel (System Process PID=4) is locking files and folders for a long time. For example when deleting a file, the file may remain locked for 1 minute or more and only after that be deleted. On another occasions I encountered files or folders I could not delete. ProcMon showed that the System Process was holding a handle to those resources for a couple of minutes and then released them None of the resources I mentioned were system resources, only files and folders installed