synchronization

Two threads using a same variable

若如初见. 提交于 2019-12-07 15:41:23
问题 I have two threads: 'main' and 'worker', and one global variable bool isQuitRequested that will be used by the main thread to inform worker , when it's time to quit its while loop (something like this: while(isQuitRequested == false) { ... do some stuff ... } ) Now, I'm a bit concerned... Do I need to use some kind of mutex protection for isQuitRequested , considering that only one thread ( main ) performs isQuitRequested = true operation, and the other ( worker ) just performs checking and

Android : Sync Adapter not running in some devices

本秂侑毒 提交于 2019-12-07 15:19:01
问题 In my android project i have a sync adapter that will sync data from android device to the server and its working fine, but recently i noticed that the onPerfomSync is not calling in my moto g3 android phone, I don't know whether there is the same problem in any other phones,I have tested it in some other Samsung phones with different android api level,but didn't find any problem there.I have configured the sync when a successful user login found.What will be the reason for this behaviour

When should each thread synchronization objects be used?

喜你入骨 提交于 2019-12-07 15:18:37
问题 Under what circumstances should each of the following synchronization objects be used? ReaderWriter lock Semaphore Mutex 回答1: Since wait() will return once for each time post() is called, semaphores are a basic producer-consumer model - the simplest form of inter-thread message except maybe signals. They are used so one thread can tell another thread that something has happened that it's interested in (and how many times), and for managing access to resources which can have at most a fixed

Is a relaxed atomic counter safe?

十年热恋 提交于 2019-12-07 14:35:47
问题 Is the following code guaranteed to return the expected value of counter (40,000,000), according to the C++11 memory model? ( NOT limited to x86). #include <atomic> #include <thread> using namespace std; void ThreadProc(atomic<int>& counter) { for (int i = 0; i < 10000000; i++) counter.fetch_add(1, memory_order_relaxed); } int main() { #define COUNT 4 atomic<int> counter = { 0 }; thread threads[COUNT] = {}; for (size_t i = 0; i < COUNT; i++) threads[i] = thread(ThreadProc, ref(counter)); for

Multi threaded java program to print even and odd numbers alternatively

人走茶凉 提交于 2019-12-07 13:55:11
问题 I was asked to write a two-threaded Java program in an interview. In this program one thread should print even numbers and the other thread should print odd numbers alternatively. Sample output: Thread1: 1 Thread2: 2 Thread1: 3 Thread2: 4 ... and so on I wrote the following program. One class Task which contains two methods to print even and odd numbers respectively. From main method, I created two threads to call these two methods. The interviewer asked me to improve it further, but I could

Delphi MREW implementation that favors readers?

你说的曾经没有我的故事 提交于 2019-12-07 13:44:58
问题 Is there a Delphi implementation of an MREW (multiple read, exclusive write) lock, that favors reading over writing? 回答1: I think the TMultiReadExclusiveWriteSynchronizer already favors readers. As the name of the component implies, a TMREWS should be used when there is much reading and little writing to be done. In addition, the READ operations should be kept to a MINIMUM otherwise your write threads could be left waiting indefinitely. It is in the SysUtils unit. 回答2: Check out

Synchronized code performs faster than unsynchronized one

余生长醉 提交于 2019-12-07 12:18:12
问题 I came out with this stunning result which i absolutely do not know the reason for: I have two methods which are shortened to: private static final ConcurrentHashMap<Double,Boolean> mapBoolean = new ConcurrentHashMap<Double, Boolean>(); private static final ConcurrentHashMap<Double,LinkedBlockingQueue<Runnable>> map = new ConcurrentHashMap<Double, LinkedBlockingQueue<Runnable>>(); protected static <T> Future<T> execute(final Double id, Callable<T> call){ // where id is the ID number of each

Spurious wakeups on windows. Is it possible?

青春壹個敷衍的年華 提交于 2019-12-07 11:40:51
问题 I recently learned "Spurious wakeups" Any people say that this problem possible only for some types of Linux PC. I use windows. I wrote test for Spurious wakeups. I got result that it is possible. But I want to show this test for you. Maybe I made mistake somewhere. my initial variant: import java.util.Random; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; public class TestSpuriousWakeups { static final int MAX_THREADS = 600; static final Object mutex = new

java and synchronization

别等时光非礼了梦想. 提交于 2019-12-07 10:03:55
问题 I am preparing for the SCJP exam and I am having trouble with fully understanding synchronization. At line 6, I have read that the thread running in main needs a lock on 'b'. Why does it need a lock on this object? My understanding is that a synchronized block of code is a protected area that only one thread can be in at any time? Moving on,the thread in main releases this lock and waits for the the thread in 'b to complete its run method. The thread in 'b' is then meant to notify the thread

Synchronized data read/write to/from main memory

本小妞迷上赌 提交于 2019-12-07 09:16:16
问题 When a synchronized method is completed, will it push only the data modified by it to main memory, or all the member variables, similarly when a synchronized method executes, will it read only the data it needs from main memory or will it clear all the member variables in the cache and read their values from main memory ? For example public class SharedData { int a; int b; int c; int d; public SharedData() { a = b = c = d = 10; } public synchronized void compute() { a = b * 20; b = a + 10; }