Tag Info

New answers tagged

1

You can use try_lock for signaling the cond_ when enqueueing. This will have a race condition that can be circumvented by making the wait timed. The idea is that if the try_lock fails then either another enqueuer is also trying to wake the dequeuers so we might as well let him do the work or the dequeuer is trying to wait and he may or may not have check ...


1

How do you know that lock-free allocators / deallocators are not used? For example, take malloc on MacOS X and iOS. Do you know it's lock free or not? (Their documentation says that calling malloc and free on the same thread in short sequence is very fast, and that's the case that you worry about).


4

Here are some possible answers: IBM has patented their lock-free allocator... But then they also support Linux etc., so they might have an incentive to at least provide an implementation. And the XMalloc guys also filed for a patent on theirs. But I haven't [yet] found any patents (or patent applications) for NBmalloc. A more a localized issue is that M. ...


2

The answer by scriptin is giving a reasonable solution, but doesn't answer your questions about the failure of the implementation you had. You get a 'queue empty' error when you call pop on a Ruby queue in non-blocking mode (true passed) and there are no items on the queue. Your "producer" thread is created and your "consumer" thread is created, but ...


1

When you want to change just one label, there is not problem with the solution you have suggested. There are, however, at least two scenarios, in what this makes sense: Synchronization. Say, you want to update a button text and enable/disable it. With runOnUiThread, there is no need for explicit synchronization or locking. Bulk update. If you are updating ...


1

There are some concerns that may prevent such an always-runInUiThread approach. The primary problem is that if you're in a non-UI thread, your operations are deferred by being posted onto a queue, so you have no idea of when your actions will actually take effect. If you have several changes that need to take effect at the same time, you need to group them ...


0

To restore the original order after they leave your thread pool, you need some sort of data structure that holds completed messages until the next expected unit arrives and then resume releasing them to the waiting consumer thread. What particular approach is best will depend upon how many messages are coming through, how sensitive the ultimate recipient ...


0

Any code using spinlocks with any noticeable amount of lock contention will perform terribly (to an extent where -- for an application like a game -- you can say "doesn't work") if the number of threads exceeds the number of cores. Imagine for example a producer thread submitting tasks to a queue which serves 4 consumer threads. There are only two cores: ...


1

Is it possible to write code (or complete software, rather than a piece of code) that won't work properly when run on a CPU that has less than N number of cores? Absolutely. The use of real-time threads would be a good example of a situation in which this is, not only possible, but the desired way (and often, the only correct way) to get the job done. ...


1

I think Joshua is heading down the right path, just not to it's conclusion. Suppose you have an architecture where there are three threads that are written to do as much as they can--when they finish what they are doing they do it again. To keep performance up these threads do not release control for anything--they don't want to risk the lag from the ...


1

In general, there is very little you can do anything with the information you gather from a task. The reason is this: once you start a task, you have to let it finish, no matter what. You can't suspend it forever. Some other parts of code might be depending on the task result; suspending it means the dependent code will be waiting forever. You can't kill ...


15

It is unlikely that these "minimum requirements" represent something below which the game will not run. Far more likely is that they represent something below which the game will not run with acceptable performance. No game company wants to deal with lots of customers complaining about crappy performance when they are running it on a single core 1 Ghz box, ...


9

Three realtime threads that never sleep and one other thread. If there are less than four cores, the fourth thread never runs. If the fourth thread needs to communicate with one of the realtime threads for the realtime thread to finish, the code will not finish with less than four cores. Obviously if realtime threads are waiting on something that doesn't ...


-1

If I understand what you're asking, it's possible, but it's a very, very bad thing. The canonical example of what you're describing would be maintaining a counter which is incremented by multiple threads. This requires almost nothing in therms of computing power but requires careful coordination among the threads. As long as only one thread at a time does ...


45

It may be possible to do this "by accident" with careless use of core affinity. Consider the following pseudocode: start a thread in that thread, find out which core it is running on set its CPU affinity to that core start doing something computationally intensive / loop forever If you start four of those on a two-core CPU, then either something goes ...


1

It could be that there are three threads doing something (generating backgrounds or generating NPC movement) and passing events to a fourth, which is supposed to aggregate/filter the events and update the view model. If the fourth thread doesn't get all the events (because it's not scheduled on a core) then the view model doesn't get updated correctly. This ...


1

Since it is possible to use virtualize to have more virtual cores than physical and the software would not know it is running on a virtualize and instead think that it does have that many physical cores, I would say such software is not possible. That is to say, it is not possible to write software that will always stop on less than N cores. As others ...


4

First of all software threads has nothing to do with hardware threads and is often mixed up. Software threads are pieces of code than can be dispatched and run on it's own within the process context. Hardware threads are mostly managed by the OS and are dispatches to the processor's core when talking about regular programs. These hardware threads are ...


34

It could be necessary to have 4 cores because the application runs four tasks in parallel threads and expects them to finish almost simultaneously. When every thread is executed by a separate core and all threads have the exact same computational workload, they are quite likely (but far from guaranteed) to finish roughly the same time. But when two threads ...


-1

Windows has built-in functionality for this: the function GetLogicalProcessorInformation is in the Windows API. You can call it from your program to get information about cores, virtual cores, and hyperthreading. So the answer to your question would be: Yes.


1

Using SwingUtilities.invokeLater() is not merely advantageous, it's essential, but to understand why you need to understand Swing's threading model. Updates to the GUI, via Swing, must occur on the Event Dispatch Thread (EDT), and code that does anything else (e.g. accessing some resource such as a database) should use one or more other threads. ...


2

There is another approach. This is similar to @ganbustein's answer, but with one difference. It is known as the Task parallelism paradigm. In task parallelism, All execution is decomposed, and the decomposed tasks are packaged into tasks. (Also known as "closures", which means a self-contained unit of executable code and all data that is needed. If any ...


3

You can't have more productive threads than you have processors. (I'm ignoring I/O for now.) Lets suppose maxThreads is the total maximum number of threads you want to have. This is a soft limit. It may get exceeded temporarily, but the excess will correct itself. Have a queue of unchecked strings, initially empty. Start one StringChecker thread. Every ...


2

Assuming that the time it takes to generate and check strings is fairly regular, you can hard code a ratio after doing some timing. Suppose generating a string takes on average N nanoseconds, and checking it takes M nanoseconds. If it takes three times as long to generate as it does to check, that is if N / M = 3, then there should be three times as many ...


0

As suggested by an answer that was deleted (unfortunately) and comments, I followed the "consistency" line (second presented variant) - in all "try...For" and "try...Until" functions the timeout (duration or time point) is the first argument. This is the code at current stage - link


2

One technique if you've got a lot of thread-unsafe code is to only let one thread have access to it. If the other threads don't touch, the lack of safety is a non-issue. To make this work, you can then communicate with the rest of the application via message queues: the thread-safe parts can ask the thread-unsafe part to do some work by sending a message to ...


3

One way to add multithreading to an older application is to use multiple processes and inter-process communication. This allows you to isolate the code in question, serializing communication with it, and if the code has bugs, memory leaks, etc, you can kill the process and restart it automatically. This works if the IPC overhead is worth the extra safety. ...



Top 50 recent answers are included