synchronization

Logging and Synchronization

情到浓时终转凉″ 提交于 2019-12-05 02:22:32
问题 I have just written my own logging framework (very lightweight, no need for a big logging framework). It consists of an interface ILogger and a number of classes implementing that interface. The one I have a question about is TGUILogger which takes a TStrings as the logging target and synchronizes the logging with the main thread so that the Items member of a listbox can be used as the target. type ILogger = Interface (IInterface) procedure Log (const LogString : String; LogLevel : TLogLevel)

How to check how many threads are waiting for a synchronized method to become unlocked

折月煮酒 提交于 2019-12-05 02:14:24
Is there any way to check how many threads are waiting for a synchronized method to become unlocked? I would like to know when the thread calls a synchronized method: 1) How many threads are already waiting to call the method? 2) Once the method is called how long it needed to wait for the method to become unlocked? Solution: I solved this using stackers answer: public class LockedClass { public static int count; public static void measuringClass() throws IOException{ long startTime = System.currentTimeMillis(); count++; System.out.println("Threads waiting="+count); lockedMethod(startTime);

How to make a fast context switch from one process to another?

瘦欲@ 提交于 2019-12-05 02:06:26
问题 I need to run unsafe native code on a sandbox process and I need to reduce bottleneck of process switch. Both processes (controller and sandbox) shares two auto-reset events and a coherent view of a mapped file (shared memory) that is used for communication. To make this article smaller, I removed initializations from sample code, but the events are created by the controller, duplicated using DuplicateHandle, and then sent to sandbox process prior to work. Controller source: void inSandbox

Why Double-Checked Locking is used at all?

拥有回忆 提交于 2019-12-05 01:53:49
问题 I keep on running across code that uses double-checked locking, and I'm still confused as to why it's used at all. I initially didn't know that double-checked locking is broken, and when I learned it, it magnified this question for me: why do people use it in the first place? Isn't compare-and-swap better? if (field == null) Interlocked.CompareExchange(ref field, newValue, null); return field; (My question applies to both C# and Java, although the code above is for C#.) Does double-checked

Incorrect synchronization in go lang

故事扮演 提交于 2019-12-05 01:50:02
问题 While I was taking a look on the golang memory model document(link), I found a weird behavior on go lang. This document says that below code can happen that g prints 2 and then 0. var a, b int func f() { a = 1 b = 2 } func g() { print(b) print(a) } func main() { go f() g() } Is this only go routine issue? Because I am curious that why value assignment of variable 'b' can happen before that of 'a'? Even if value assignment of 'a' and 'b would happen in different thread(not in main thread),

cuda, dummy/implicit block synchronization

点点圈 提交于 2019-12-05 01:34:35
问题 I am aware that block sync is not possible, the only way is launching a new kernel. BUT, let's suppose that I launch X blocks, where X corresponds to the number of the SM on my GPU. I should aspect that the scheduler will assign a block to each SM...right? And if the GPU is being utilized as a secondary graphic card (completely dedicated to CUDA), this means that, theoretically, no other process use it... right? My idea is the following: implicit synchronization. Let's suppose that sometimes

Correctly synchronizing equals() in Java

孤街醉人 提交于 2019-12-05 01:10:48
I have the following class which contains only one field i . Access to this field is guarded by the lock of the object ("this"). When implementing equals() I need to lock this instance (a) and the other (b). If thread 1 calls a.equals(b) and at the same time thread 2 calls b.equals(a), the locking order is reverse in the two implementations and may result in deadlock. How should I implement equals() for a class which has synchronized fields? public class Sync { // @GuardedBy("this") private int i = 0; public synchronized int getI() {return i;} public synchronized void setI(int i) {this.i = i;}

Preferring synchronized to volatile

ぃ、小莉子 提交于 2019-12-05 00:52:17
I've read this answer in the end of which the following's written: Anything that you can with volatile can be done with synchronized, but not vice versa. It's not clear. JLS 8.3.1.4 defines volatile fields as follows: A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4). So, the volatile fields are about memory visibility. Also, as far as I got from the answer I cited, reading and writing to volatile fields are synched. Synchronization, in turn guarantees that the only one thread has access to a synched

Loop over multiple UIAlertController's

我只是一个虾纸丫 提交于 2019-12-05 00:22:11
问题 In some cases my applications needs to display muliple Alert messages. Error messages are gathered on start and needs to be displayed to the user one at a time. When the first one is acknowledged, the next one should be presented. The problem is that they all try to execute at the same time, obviously. Is there a smart way to do this synchronously? Here is some code that simply describes what I want to do: var errors : [NSError]! override func viewDidLoad() { super.viewDidLoad() // Do any

C pthread synchronize function

时光总嘲笑我的痴心妄想 提交于 2019-12-05 00:00:14
Is there a function in pthread library to synchronize threads? Not mutexes, not semaphores, just one call function. It is supposed to lock the threads that get in that point until all the threads reach such function. E.g: function thread_worker(){ //hard working syncThreads(); printf("all threads are sync\n"); } So the printf is called only when all the threads end the hard working. The proper way to do this would be with a barrier . pthread supports barriers using pthread_barrier_t . You initialize it with the number of threads that will need to sync up, and then you just use pthread_barrier