thread-synchronization

Synchronized implementation : Java

末鹿安然 提交于 2019-12-11 02:35:30
问题 This is a Consumer-Producer problem in which, I wish to get output as follows: Put: 0 Get: 0 Put: 1 Get: 1 ....and so on. But in contrast to this, the Consumer class consumes same value of q multiple times, inspite of using wait() and notify() methods.. as well as the Producer class overruns the consumer. How can I get synchronized output? This is QFixed class:(which defines put() and get() methods) class QFixed{ int n; boolean valueset = false; synchronized int get(){ if(!valueset){ try {

std::timed_mutex::try_lock* fail spuriously

谁说我不能喝 提交于 2019-12-06 19:39:20
问题 By try_lock* , I take to mean try_lock() , try_lock_for() , and try_lock_until() . According to cppreference, all three methods may just fail spuriously. Following is quoted from the description for try_lock_for() As with try_lock() , this function is allowed to fail spuriously and return false even if the mutex was not locked by any other thread at some point during timeout_duration . I know that spurious wakeup may happen with std::condition_variable and the rationale behind it. But, what

Synchronized block in the main method

心不动则不痛 提交于 2019-12-06 14:12:20
问题 In the below code about the synchronisation between threads, according to the output generated why is the control being transferred to the execution of the new thread despite the lock being acquired for the same object "dt" in the main method ? public class DemoThread extends Thread { public DemoThread() { } public void run() { int i=0; synchronized(this) { while(++i<=5) { sum=i; try{ sleep(1000); System.out.println("Woke up from sleep"); if(i>=2) this.notify(); }catch(InterruptedException ie

Synchronizing elements in an array

浪尽此生 提交于 2019-12-06 08:39:34
I am new to multi-threading in Java and don't quite understand what's going on. From online tutorials and lecture notes, I know that the synchronized block, which must be applied to a non-null object, ensures that only one thread can execute that block of code. Since an array is an object in Java, synchronize can be applied to it. Further, if the array stores objects, I should be able to synchronize each element of the array too. My program has several threads updated an array of numbers, hence I created an array of Long objects: synchronized (grid[arrayIndex]){ grid[arrayIndex] += a.getNumber

std::timed_mutex::try_lock* fail spuriously

佐手、 提交于 2019-12-05 00:43:25
By try_lock* , I take to mean try_lock() , try_lock_for() , and try_lock_until() . According to cppreference , all three methods may just fail spuriously. Following is quoted from the description for try_lock_for() As with try_lock() , this function is allowed to fail spuriously and return false even if the mutex was not locked by any other thread at some point during timeout_duration . I know that spurious wakeup may happen with std::condition_variable and the rationale behind it. But, what is the case with a mutex? According to: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3209

understanding of Volatile.Read/Write

喜你入骨 提交于 2019-12-05 00:11:56
I'm trying to understand the C# Volatile class. As i read: The Volatile.Write method forces the value in location to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to Volatile.Write. The Volatile.Read method forces the value in location to be read from at the point of the call. In addition, any later program-order loads and stores must occur after the call to Volatile.Read. Is that means the in the case of: internal sealed class ThreadsSharingData { private Int32 m_flag = 0; private Int32 m_value = 0; // This method is

Synchronization mechanism for an observable object

社会主义新天地 提交于 2019-12-03 22:27:10
Let's imagine we have to synchronize read/write access to shared resources. Multiple threads will access that resource both in read and writing (most of times for reading, sometimes for writing). Let's assume also that each write will always trigger a read operation (object is observable). For this example I'll imagine a class like this (forgive syntax and style, it's just for illustration purposes): class Container { public ObservableCollection<Operand> Operands; public ObservableCollection<Result> Results; } I'm tempted to use a ReadWriterLockSlim for this purpose moreover I'd put it at

What is the difference between Thread.join and Synchronized?

余生长醉 提交于 2019-12-03 05:57:32
I am confused when to use Thread.join() and when to use synchronization in multi threading application. According to me, both of them block or wait for the execution to be done by some other thread. This example has to output 10 A's , 10 B's & 10 C's in sequential pattern one after other like : 1 : A 2 : A 3 : A 4 : A 5 : A 6 : A 7 : A 8 : A 9 : A 10 : A 1 : B 2 : B 3 : B 4 : B 5 : B 6 : B 7 : B 8 : B 9 : B 10 : B 1 : C 2 : C 3 : C 4 : C 5 : C 6 : C 7 : C 8 : C 9 : C 10 : C ----ProGraM ENDS---- Example starts here class SyncTest extends Thread { StringBuffer sb; public SyncTest(StringBuffer sb

compare and swap vs test and set

余生颓废 提交于 2019-12-03 03:13:52
问题 Could someone explain to me the working and differences of above operations in multi-threading? 回答1: test-and-set modifies the contents of a memory location and returns its old value as a single atomic operation. compare-and-swap atomically compares the contents of a memory location to a given value and, only if they are the same , modifies the contents of that memory location to a given new value. The difference marked in bold. 回答2: Test and set operates on a bit, compare and swap operates

compare and swap vs test and set

不问归期 提交于 2019-12-02 16:44:19
Could someone explain to me the working and differences of above operations in multi-threading? test-and-set modifies the contents of a memory location and returns its old value as a single atomic operation. compare-and-swap atomically compares the contents of a memory location to a given value and, only if they are the same , modifies the contents of that memory location to a given new value. The difference marked in bold. Test and set operates on a bit, compare and swap operates on a 32-bit field. The z/TPF system favors the use of the test and set (TS) instruction because frequently, lock