synchronization

Wait until all Task finish in unit test

泪湿孤枕 提交于 2019-11-29 07:11:34
问题 I have this class I want to unit test: public class SomeClass { public void Foo() { Bar(); } private void Bar() { Task.Factory.StartNew(() => { // Do something that takes some time (e.g. an HTTP request) }); } } And this is how my unit test looks like: [TestMethod] public void TestFoo() { // Arrange var obj = new SomeClass(); // Act obj.Foo(); obj.Foo(); obj.Foo(); // Assert /* I need something to wait on all tasks to finish */ Assert.IsTrue(...); } So, I need to make the unit test thread

Changing the locking object inside @synchronized section

假装没事ソ 提交于 2019-11-29 07:10:54
Can I do any of the following? Will they properly lock/unlock the same object? Why or why not? Assume there are many identical threads using global variable "obj", which was initialized before all threads started. 1. @synchronized(obj) { [obj release]; obj = nil; } 2. @synchronized(obj) { obj = [[NSObject new] autorelease]; } Short answer: no, they won't properly lock/unlock, and such approaches should be avoided. My first question is why you'd want to do something like this, since these approaches nullify the purposes and benefits of using a @synchronized block in the first place. In your

Why does __sync_add_and_fetch work for a 64 bit variable on a 32 bit system?

血红的双手。 提交于 2019-11-29 06:43:03
问题 Consider the following condensed code: /* Compile: gcc -pthread -m32 -ansi x.c */ #include <stdio.h> #include <inttypes.h> #include <pthread.h> static volatile uint64_t v = 0; void *func (void *x) { __sync_add_and_fetch (&v, 1); return x; } int main (void) { pthread_t t; pthread_create (&t, NULL, func, NULL); pthread_join (t, NULL); printf ("v = %"PRIu64"\n", v); return 0; } I have a uint64_t variable that I want to increment atomically, because the variable is a counter in a multi-threaded

C#'s lock() in Managed C++

冷暖自知 提交于 2019-11-29 05:48:56
问题 Does managed C++ have an equivalent to C#'s lock() and VB's SyncLock? If so, how do I use it? 回答1: The equivelent to a lock / SyncLock would be to use the Monitor class. In .NET 1-3.5sp, lock(obj) does: Monitor.Enter(obj); try { // Do work } finally { Monitor.Exit(obj); } As of .NET 4, it will be: bool taken = false; try { Monitor.Enter(obj, ref taken); // Do work } finally { if (taken) { Monitor.Exit(obj); } } You could translate this to C++ by doing: System::Object^ obj = gcnew System:

How to run two functions simultaneously

两盒软妹~` 提交于 2019-11-29 05:45:49
I am running test but I want to run 2 functions at the same time. I have a camera and I am telling it to move via suds, I am then logging into the camera via SSH to check the speed the camera is set to. When I check the speed the camera has stopped so no speed is available. Is there a way I can get these functions to run at the same time to test the speed of the camera. Sample code is below: class VerifyPan(TestAbsoluteMove): def runTest(self): self.dest.PanTilt._x=350 # Runs soap move command threading.Thread(target = SudsMove).start() self.command = './ptzpanposition -c 0 -u degx10' # Logs

Recursive / nested locking in C# with the lock statement [duplicate]

混江龙づ霸主 提交于 2019-11-29 05:42:53
Possible Duplicate: Re-entrant locks in C# I've looked here on StackOverflow and on MSDN , and can't believe that I couldn't find this question lingering out there on the internets. Let's say I have a class with a private member that I want to access in several public methods. These public methods will be called by different threads, hence the need for synchronization. public class MyClass { private Object SomeSharedData = new Object(); public void MethodA() { lock( SomeSharedData) { // do something MethodB(); } } public void MethodB() { lock( SomeSharedData) { // do something } } } Note that

Waiting win32 threads

孤人 提交于 2019-11-29 05:37:54
问题 I have a totally thread-safe FIFO structure( TaskList ) to store task classes, multiple number of threads, some of which creates and stores task and the others processes the tasks. TaskList class has a pop_front() method which returns the first task if there is at least one. Otherwise it returns NULL . Here is an example of processing function: TaskList tlist; unsigned _stdcall ThreadFunction(void * qwe) { Task * task; while(!WorkIsOver) // a global bool to end all threads. { while(task =

What does 'result' in ExecutorService.submit(Runnable task, T result) do?

社会主义新天地 提交于 2019-11-29 05:32:34
Looking at the javadocs it just says <T> Future<T> submit(Runnable task, T result) Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return the given result upon successful completion. Parameters: task - the task to submit result - the result to return but what does it do with result? does it store anything there? does it just use the type of result to specify the type of Future<T> ? It doesn't do anything with the result - just holds it. When the task successfully completes, calling future.get() will return the result you passed in

synchronized object set to null

天大地大妈咪最大 提交于 2019-11-29 05:17:32
问题 I have two threads Thread1 and Thread2 //Within Thread1 synchronized(obj1) { obj1 = null; } //Within Thread2 synchronized(obj1) { do something } If jvm first executes thread1 and sets obj1 to null, then will thread2 see that change immediately or will it take time and jvm could still run the thread2 synchronized block since obj1 is not yet null? 回答1: This will almost certainly break the synchronization abstraction -- I wouldn't be confident that thread2 will see the change immediately. You

What are synchronizing strategies for Prevayler?

一世执手 提交于 2019-11-29 04:46:47
Prevayler guarantees that all the writes ( through its transactions) are synchronized. But what about reads? Is it right that dirty reads are possible if no explicit synchronizing is used (in user code)? Are they possible if a business object is read as: // get the 3rd account Accont account = (Bank)prevayler.prevalentSystem().getAccounts().get(2); ? If so what synchronizing strategies are good for a user code? (Consider a business object A contains a collection of business objects Bs), using a synchronized collection (of Bs inside of A), for example from java.util.concurrent package?