synchronization

Thread and Queue

只谈情不闲聊 提交于 2019-11-29 23:00:01
I am interested in knowing what would be the best way to implement a thread based queue. For example: I have 10 actions which I want to execute with only 4 threads. I would like to create a queue with all the 10 actions placed linearly and start the first 4 action with 4 threads, once one of the thread is done executing, the next one will start etc - So at a time, the number of thread is either 4 or less than 4. Theo There is a Queue class in thread in the standard library. Using that you can do something like this: require 'thread' queue = Queue.new threads = [] # add work to the queue queue

Critical sections with multicore processors

假如想象 提交于 2019-11-29 22:20:28
With a single-core processor, where all your threads are run from the one single CPU, the idea of implementing a critical section using an atomic test-and-set operation on some mutex (or semaphore or etc) in memory seems straightforward enough; because your processor is executing a test-and-set from one spot in your program, it necessarily can't be doing one from another spot in your program disguised as some other thread. But what happens when you do actually have more than one physical processor? It seems that simple instruction level atomicity wouldn't be sufficient, b/c with two processors

“select” on multiple Python multiprocessing Queues?

≡放荡痞女 提交于 2019-11-29 22:19:11
What's the best way to wait (without spinning) until something is available in either one of two (multiprocessing) Queues , where both reside on the same system? It doesn't look like there's an official way to handle this yet. Or at least, not based on this: http://bugs.python.org/issue3831 You could try something like what this post is doing -- accessing the underlying pipe filehandles: http://haltcondition.net/?p=2319 and then use select. Actually you can use multiprocessing.Queue objects in select.select. i.e. que = multiprocessing.Queue() (input,[],[]) = select.select([que._reader],[],[])

How to synchronize development and production database

南笙酒味 提交于 2019-11-29 22:10:15
Do you know any applications to synchronize two databases - during development sometimes it's required to add one or two table rows or new table or column. Usually I write every sql statement in some file and during uploading path I evecute those lines on my production database (earlier backing it up). I work with mySQL and postreSQL databases. What is your practise and what applications helps you in that. You asked for a tool or application answer, but what you really need is a a process answer. The underlying theme here is that you should be versioning your database DDL (and DML, when needed

Is mutex needed to synchronize a simple flag between pthreads?

孤人 提交于 2019-11-29 21:50:52
Let's imagine that I have a few worker threads such as follows: while (1) { do_something(); if (flag_isset()) do_something_else(); } We have a couple of helper functions for checking and setting a flag: void flag_set() { global_flag = 1; } void flag_clear() { global_flag = 0; } int flag_isset() { return global_flag; } Thus the threads keep calling do_something() in a busy-loop and in case some other thread sets global_flag the thread also calls do_something_else() (which could for example output progress or debugging information when requested by setting the flag from another thread). My

How can I synchronize two processes accessing a file on a NAS?

这一生的挚爱 提交于 2019-11-29 21:37:03
问题 Here's the thing: I have two applications, written in C++ and running on two machines with different OS (one Linux and one Windows). One of this process is in charge of updating an XML file on a NAS (Network Attached Storage) while the other one reads this file. Is it possible to synchronize these two processes in order to avoid reading of the file at the same time it's being modified? 回答1: You could create a lock file on the server that is created before you do a write, wait then write and

Definition of “synchronization primitive”

a 夏天 提交于 2019-11-29 20:27:11
What exactly does the term synchronization primitive mean? For example: mutex, critical section, waitable timer, event, monitor, conditional variable, semaphore. Are all of them synchronization primitives? Are there any other synchronization primitives I have not listed? And are these a valid questions? Synchronization primitives are simple software mechanisms provided by a platform (e.g. operating system) to its users for the purposes of supporting thread or process synchronization. They're usually built using lower level mechanisms (e.g. atomic operations, memory barriers, spinlocks, context

Automatically keep a secondary repo in sync with a primary repo?

送分小仙女□ 提交于 2019-11-29 20:18:57
We have a two tier setup. We have a primary repository (called 'primary' below). And a secondary repository (called 'secondary' below) that was created like so: $ git clone --bare --shared $REPO_A/primary secondary.git People working on the secondary repository view the branches which originated from the primary repository as read only but base their own branches off these branches. We want to sync up the secondary repository with the primary repository once a day. I.e. we want commits and new branches that were pushed to the primary to become visible to people working off the secondary

Is Critical Section always faster?

回眸只為那壹抹淺笑 提交于 2019-11-29 19:54:44
I was debugging a multi-threaded application and found the internal structure of CRITICAL_SECTION . I found data member LockSemaphore of CRITICAL_SECTION an interesting one. It looks like LockSemaphore is an auto-reset event (not a semaphore as the name suggests) and operating system creates this event silently when first time a thread waits on Critcal Section which is locked by some other thread. Now, I am wondering is Critical Section always faster? Event is a kernel object and each Critical section object is associated with event object then how Critical Section can be faster compared to

Need to manually synchronize the Synchronized list while iteration when it could be avoided?

人盡茶涼 提交于 2019-11-29 19:44:40
问题 My question is about synchronizedList method Collections Class. Javadocs say: It is imperative that the user manually synchronize on the returned list when iterating over it: List list = Collections.synchronizedList(new ArrayList()); ... synchronized(list) { Iterator i = list.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } Though manually synchroniziation is not required for other methods. I looked into the source code of Collections class and found