synchronization

Can Test and Set be implemented in software without hardware support?

人盡茶涼 提交于 2019-12-08 19:53:19
问题 Here's the Test and Set written in software: boolean TestAndSet(boolean *target) { boolean rv = *target; *target = TRUE; return rv; } and do { while(TestAndSetLock(&lock)) ; // do nothing // critical section lock = FALSE; // remainder section } while(TRUE); Can we use the mechanism in CPUs that do not support test-and-set at the hardware level? If so, how is atomicity assured? 回答1: You can use Lamport's 'bakery' mutual exclusion algorithm on machines w/o TAS/CAS to gate access to the 'atomic'

.NET Multithreading - Do I need to synchronise access to a variable of primitive type?

China☆狼群 提交于 2019-12-08 17:41:32
问题 The scenario I have a class with a bool Enabled property, that is used by a loop on another thread to see whether it should stop or not. The idea is that a different thread can set that property to false and stop the other thread running cleanly. The question Should I bother to serialise access to that Enabled property using something like lock (lockObject) { ... } , or is it safe without? 回答1: Primitive type reads are atomic provided that they fit within a CPU read. Thus a 32 bit primitive

Thread syncronization

故事扮演 提交于 2019-12-08 15:26:24
问题 I am developing an application in which I have 3 threads. Let us call them a,b,c. Now I have to develop something like this. Initially b waits for a to complete its task, and c waits for b. As soon as 'a' finishes its task it notifies 'b'. 'b' should wake up. Now 'a' goes to wait state. 'a' will wait until it gets an acknowledgement from 'c'. Now b finishes its task and notifies 'c'. Now 'c' wakes up and 'b' goes to wait state. Now c finishes the task and acknowledges to 'a'. Now 'c' goes to

Greater-than compare-and-swap

≯℡__Kan透↙ 提交于 2019-12-08 14:45:53
问题 As the title suggests, I'm looking for a compare-and-swap implementation, but with greater-than comparison: if(newValue > oldValue) { oldValue = newValue; } where oldValue is some global shared state and newValue is private to each thread, without doing this: synchronized(locker) { if(newValue > oldValue) { oldValue = newValue; } } because I want a non-blocking solution. From studying source codes of other non-blocking operations, I've come up with this (assuming the values are integers):

Scrapy: how to populate hierarchic items with multipel requests

江枫思渺然 提交于 2019-12-08 12:23:27
问题 This one is extension of Multiple nested request with scrapy . Asking because presented solution have flaws: 1. It iliminates asynchrony, thus heavily reducing scraping efficiency 2. Should exception appeare while processing links "stack" and no item will be yelded 3. What if there is a huge amount of child items? To deal with (1) I considered this: class CatLoader(ItemLoader): def __int__(self, item=None, selector=None, response=None, parent=None, **context): super(self.__class__, self)._

How can I get the invoices that has been paid on an specific date from QuickBooks to .NET app?

亡梦爱人 提交于 2019-12-08 11:52:24
问题 I am trying to get a list with all the invoices from QuickBooks that has been paid on an specific date. I found in a blog the way to get all the invoices from QuickBooks. bool sessionBegun = false; bool connectionOpen = false; QBSessionManager sessionManager = null; try { //Create the session Manager object sessionManager = new QBSessionManager(); //Create the message set request object to hold our request IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 8, 0);

InterlockedIncrement vs InterlockedIncrementAcquire / Release (Redux)

为君一笑 提交于 2019-12-08 10:44:30
问题 This is a followup to this question: I guess I don't understand the Interlocked Acquire / Release APIs. I put together the small program below. As I understand it, g_val_1, g_val_2 and g_val_3 should always be updated in the same order and should end up as all the same value. But they do not (for more than one thread). What am I missing? Thanks. #include "windows.h" #include "stdio.h" #define _THREADS_ 100 #define _TICKS_ 1000 int volatile g_threads = 0; DWORD volatile g_val_1 = 0; DWORD

Java synchronized and static synchronized method accessing static field

ぐ巨炮叔叔 提交于 2019-12-08 10:28:35
问题 What would be the behaviour of the following program where static synchronized method and instance synchronized method is trying to access static field of same class in different threads? Will any thread get blocked? Its very confusing. class MyClass { public static int i = 5; public synchronized void m1() { System.out.println(i); //uses static field i of MyClass //T1 is executing this method } public static synchronized void m3() { //T2 will be able to call this method on same object lock

How do you find the Main Window thread in UWP?

六眼飞鱼酱① 提交于 2019-12-08 10:10:10
问题 I have a library that needs to call the main windows thread in UWP when it receives a message from a communication channel (let say, a message indicating a check box should be checked). The library saves a copy of the SynchronizationContext when it starts up, so it can Post back to it when the data comes in, but the trouble is, I'm not guaranteed that the caller of this library is on the main thread when it's initialized. Is there some simple, guaranteed way to obtain the Main Window Thread

Sync Local Microsoft MySQL database to remote mysql database scheduled daily

耗尽温柔 提交于 2019-12-08 09:45:52
问题 Hi i have a local mysql database that gets updated locally daily on windows machine. at the end of the day, i want a scheduled job to be called by windows that updates a remote mysql database with all the the new data ie compare and synchronize. i have already created the test database on remote server and ive allowed remote access. i want simple program/solution that i could call on command line that updates remote database with new or changed records etc. the local database is on a windows