synchronization

Android application with phone book synchronization?

牧云@^-^@ 提交于 2019-11-28 00:36:24
I am creating one android test application , in which i have one button. On button click ,i want to synchronize phonebook records with my local database.If record in phone book is not in db table then insert it , otherwise leave it as it is. so how can i do this? For getting contact list from your phone book you need write permission in AndroidManifest.XML (i.e. android.permission.READ_CONTACTS ) .And you can collect contact list using following method. ShowContact() { ArrayList<String> nameList; ArrayList<String> phoneNoList; ContentResolver cr = getContentResolver(); Cursor cur = cr.query

Proper way for signal edge detection in Verilog

喜你入骨 提交于 2019-11-28 00:15:53
I want to detect a rising edge of a signal from a flip-flop AA to BB +----+ A ----------------| |----- OUT +----+ | BB | B ----| |------|> | | AA | +----+ clk ----|> | +----+ Verilog code: module edge_detect ( input A, input B, input clk, output OUT ); reg AA; reg BB; always @(posedge clk) begin AA <= B; end always @(posedge AA)begin BB <= A; end assign OUT = BB; endmodule The output of AA is used as a clock to BB saying that AA has done its job and then BB can now continue its operation. I rarely see this code. Is this a good practice? If not, are there any other proper way to detect an edge

Program using Semaphores runs fine on Linux…unexpected results on Mac osX

痴心易碎 提交于 2019-11-27 23:06:40
I wrote a simple program solving the Readers-Writers problem using semaphores. It runs perfectly on Linux os, but when I run it on my Mac osX I get unexpected results and I can't figure out why. My Program: #include <semaphore.h> #include <sys/types.h> #include <stdio.h> #include <pthread.h> #include <unistd.h> void* function1(void* val); void* function2(void* val); // shared values volatile int X; volatile int Y; // declare semaphores sem_t s1; sem_t s2; main() { void* status; pthread_t thread1; pthread_t thread2; srand(time(NULL)); // initialize semaphores to zero sem_init(&s1, 0, 0); sem

What is the difference between synchronized and static synchronized?

浪子不回头ぞ 提交于 2019-11-27 21:45:12
For a travel booking web application, where there are 100 concurrent users logged in, should ticket booking and generating an "E-Ticket Number" be implemented by a "synchronized" or a "static synchronized" method? Well, are you aware of the difference between a static method and an instance method in general? The only difference that synchronized makes is that before the VM starts running that method, it has to acquire a monitor. For an instance method, the lock acquired is the one associated with the object you're calling the method on. For a static method, the lock acquired is associated

Does thread.yield() lose the lock on object if called inside a synchronized method?

∥☆過路亽.° 提交于 2019-11-27 21:25:29
问题 I understand that Thread.currentThread().yield() is a notification to thread scheduler that it may assign cpu cycle to some other thread of same priority if any such is present. My question is: If current thread has got lock on some object and calls yield() , will it loses that lock right away? And when thread scheduler finds out there is no such thread to assign cpu cycle, then the thread which has called yield() will again be in fight to get lock on the object which it has lost earlier?? I

Sharing a variable between multiple different threads

梦想与她 提交于 2019-11-27 21:20:55
I want to share a variable between multiple threads like this: boolean flag = true; T1 main = new T1(); T2 help = new T2(); main.start(); help.start(); I'd like to share flag between main and help thread where these are two different Java classes I've created. Is any way to do this? Thanks! Brian Agnew Both T1 and T2 can refer to a class containing this variable. You can then make this variable volatile , and this means that Changes to that variable are immediately visible in both threads. See this article for more info. Volatile variables share the visibility features of synchronized but none

Please explain initialization safety as spelled out in Java memory model

我只是一个虾纸丫 提交于 2019-11-27 20:34:02
问题 Can some one explain initialization safety as required by Java memory model ? How does the final fields help in achieving initialization safety ? What role does the constructor play in ensuring initialization safety ? 回答1: Initialization safety provides for an object to be seen by an external thread in its fully constructed ( initialized ) state. The prerequisite is that the object should not be published prematurely ie. in its constructor. Once this is ensured , JMM requires certain

x86 spinlock using cmpxchg

…衆ロ難τιáo~ 提交于 2019-11-27 20:22:56
问题 I'm new to using gcc inline assembly, and was wondering if, on an x86 multi-core machine, a spinlock (without race conditions) could be implemented as (using AT&T syntax): spin_lock: mov 0 eax lock cmpxchg 1 [lock_addr] jnz spin_lock ret spin_unlock: lock mov 0 [lock_addr] ret 回答1: You have the right idea, but your asm is broken: cmpxchg can't work with an immediate operand, only registers. lock is not a valid prefix for mov . mov to an aligned address is atomic on x86, so you don't need lock

wait and notify in C/C++ shared memory

最后都变了- 提交于 2019-11-27 20:10:30
问题 How to wait and notify like in Java In C/C++ for shared memory between two or more thread?I use pthread library. 回答1: Instead of the Java object that you would use to wait/notify, you need two objects: a mutex and a condition variable. These are initialized with pthread_mutex_init and pthread_cond_init . Where you would have synchronized on the Java object, use pthread_mutex_lock and pthread_mutex_unlock (note that in C you have to pair these yourself manually). If you don't need to wait

Running code on the main thread from a secondary thread?

試著忘記壹切 提交于 2019-11-27 20:05:26
This is a general Java question and not an Android one first off! I'd like to know how to run code on the main thread, from the context of a secondary thread. For example: new Thread(new Runnable() { public void run() { //work out pi to 1,000 DP (takes a while!) //print the result on the main thread } }).start(); That sort of thing - I realise my example is a little poor since in Java you don't need to be in the main thread to print something out, and that Swing has an event queue also - but the generic situation where you might need to run say a Runnable on the main thread while in the