locking

Recursive / nested locking in C# with the lock statement [duplicate]

♀尐吖头ヾ 提交于 2019-11-30 07:56:47
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Re-entrant locks in C# I've looked here on StackOverflow and on MSDN, and can't believe that I couldn't find this question lingering out there on the internets. Let's say I have a class with a private member that I want to access in several public methods. These public methods will be called by different threads, hence the need for synchronization. public class MyClass { private Object SomeSharedData = new

Why Locking On a Public Object is a Bad Idea

自作多情 提交于 2019-11-30 07:56:29
问题 Ok, I've used locks quite a bit, but I've never had this scenario before. I have two different classes that contain code used to modify the same MSAccess database: public class DatabaseNinja { public void UseSQLKatana { //Code to execute queries against db.TableAwesome } } public class DatabasePirate { public void UseSQLCutlass { //Code to execute queries against db.TableAwesome } } This is a problem, because transactions to the database cannot be executed in parallel, and these methods

How do I lock the console across threads in C#.NET?

心已入冬 提交于 2019-11-30 07:13:02
I have a logger class that handles various information display with pretty colors (yay.). However, since it writes to the console in separated steps ( i.e. set color to red, write text, set color to gray, write text, for something that would render "[Error] Description..." with the error being in red ), but I have a multithreaded application, so the steps can get mixed up and print random stuff in random colors. I am aware of the lock keyword, however it will not work with a static class such as the console. Here is some example code if I was unclear: using System; using System.Text; namespace

How do I lock read/write to MySQL tables so that I can select and then insert without other programs reading/writing to the database?

时间秒杀一切 提交于 2019-11-30 06:35:16
问题 I am running many instances of a webcrawler in parallel. Each crawler selects a domain from a table, inserts that url and a start time into a log table, and then starts crawling the domain. Other parallel crawlers check the log table to see what domains are already being crawled before selecting their own domain to crawl. I need to prevent other crawlers from selecting a domain that has just been selected by another crawler but doesn't have a log entry yet. My best guess at how to do this is

Massive fprintf speed difference without “-std=c99”

限于喜欢 提交于 2019-11-30 06:26:20
I had been struggling for weeks with a poor-performing translator I had written. On the following simple bechmark #include<stdio.h> int main() { int x; char buf[2048]; FILE *test = fopen("test.out", "wb"); setvbuf(test, buf, _IOFBF, sizeof buf); for(x=0;x<1024*1024; x++) fprintf(test, "%04d", x); fclose(test); return 0 } we see the following result bash-3.1$ gcc -O2 -static test.c -o test bash-3.1$ time ./test real 0m0.334s user 0m0.015s sys 0m0.016s As you can see, the moment the "-std=c99" flag is added in, performance comes crashing down: bash-3.1$ gcc -O2 -static -std=c99 test.c -o test

Thread-safe memoization

送分小仙女□ 提交于 2019-11-30 05:55:32
问题 Let's take Wes Dyer's approach to function memoization as the starting point: public static Func<A, R> Memoize<A, R>(this Func<A, R> f) { var map = new Dictionary<A, R>(); return a => { R value; if (map.TryGetValue(a, out value)) return value; value = f(a); map.Add(a, value); return value; }; } The problem is, when using it from multiple threads, we can get into trouble: Func<int, int> f = ... var f1 = f.Memoize(); ... in thread 1: var y1 = f1(1); in thread 2: var y2 = f1(1); // We may be

How to prevent two CUDA programs from interfering

穿精又带淫゛_ 提交于 2019-11-30 05:42:34
问题 I've noticed that if two users try to run CUDA programs at the same time, it tends to lock up either the card or the driver (or both?). We need to either reset the card or reboot the machine to restore normal behavior. Is there a way to get a lock on the GPU so other programs can't interfere while it's running? Edit OS is Ubuntu 11.10 running on a server. While there is no X Windows running, the card is used to display the text system console. There are multiple users. 回答1: If you are running

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

℡╲_俬逩灬. 提交于 2019-11-30 05:30:50
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. Here is what I tried, but it doesn't appear to work. $mysqli = new mysqli(DB_HOST, DB_USER, DB

Efficient transaction, record locking

梦想与她 提交于 2019-11-30 05:27:11
问题 I've got a stored procedure, which selects 1 record back. the stored procedure could be called from several different applications on different PCs. The idea is that the stored procedure brings back the next record that needs to be processed, and if two applications call the stored proc at the same time, the same record should not be brought back. My query is below, I'm trying to write the query as efficiently as possible (sql 2008). Can it get done more efficiently than this? CREATE

How to properly avoid Mysql Race Conditions

烂漫一生 提交于 2019-11-30 05:22:58
问题 I know this has been asked before, but I'm still confused and would like to avoid any problems before I go into programming if possible. I plan on having an internal website with at least 100 users active at any given time. Users would post an item (inserted into db with a 0 as its value) and that item would be shown via a php site (db query). Users then get the option to press a button and lock that item as theirs (assign the value of that item as their id) How do I ensure that 2 or more