One needed to write a game with player A (possibly through input, but restrict to automated code for simplicity) and player B.
A and B were say on a shared array array
int Map[100][100]
One way was to write a sequential code, which was not interesting at all. Especially, with the keyboard input, there might be "the stuck" and "the missed strikes".
An idea way to fully allow for computational power and simultaneous updates was to use parallel process for player A and player B, such as a parallel_for loop inside a while loop or a while loop inside a parallel_for loop.
However, this brought to another question about the shared memory usage as mentioned above. A way to went around it was to use fstream to set a longer delay time and to write and read to disk. But that obviously would cause troubles and possible crashes, as well as the delayed computational efficiencies. (Will references by array also cause trouble if the memories were accessed at the same time? or has that been taken care by the hardware design of the CPU-Memory bridges?)
How to write a game with parallel possesses that against each other with shared memories/arrays?
sudo language:
player_script(indices, ){while(condition){}}
main(){
parallel_for(ix){player_script(ix, );}
}
I am programming a robot with QT (C++).
I have a function move_gripper() which closes or opens the gripper (it depends on the parameter) and a function relative_motion() which lets the end effector move downwards. What I want to do is to close the gripper ( calling the move_gripper() function ) while moving the robot downwards ( calling the relative_motion() function )
The execution time of the functions are not the same ( let's say move_gripper() lasts 2 seconds while relative_motions() lasts 3 seconds ). I want the two functions to start the execution at the same time.
In computer theory there is no real "simultaneously".
The common approach is to use threads. Those threads are executed, halted, switched just so fast it looks like they are executed simultaneously.
But still there are some things which can be done wrong which make the usage of threads even worse than not to use them.
So I would say first get some knowledge about threads (see the link: https://doc.qt.io/qt-5/qthread.html) and create 2 threads, one for each channel and do your work in there.
I have just started using threads when coding, and I'm not sure whether I understood properly how they work.
If I got it right, with a thread you can make two functions run at the same time. Is it possible to change the value given to one of the functions while it is still running in parallel?
In my case I read instructions from a csv file such as:
colAction=VELOCITY; colTime=0; colParam1=-30; colParam2=2;
colAction=VELOCITY; colTime=10; colParam1=-15; colParam2=0.4;
colAction=VELOCITY; colTime=0; colParam1=-10; colParam2=1;
colAction=VELOCITY; colTime=45; colParam1=-60; colParam2=11;
colAction=TEMPERATURE; colTime=120; colParam1=95;
colAction=TEMPERATURE; colTime=20; colParam1=57;
colAction=TEMPERATURE; colTime=25; colParam1=95;
colAction=LOOP; colParam1=22; colParam2=7; colParam3=23;
colAction=TEMPERATURE; colTime=20; colParam1=95;
colAction=VELOCITY; colTime=0; colParam1=-10; colParam2=11;
colAction=VELOCITY; colTime=1; colParam1=-1; colParam2=5;
colAction=VELOCITY; colTime=5; colParam1=-20; colParam2=11;
I have a function that sets a temperature and a function that sets a velocity. the parameter colTime tells me how long I have to hold the velocity or the temperature, without following the next instruction. when the colTime has expired, I need to follow the next instruction: if a temperature is followed by another temperatur I just give the function the next value, but if temperature is followed by velocity, I need to keep the temperature function running, while starting the velocity function.
The problem arises, when after setting temperature then velocity follows another temperature. Now I need to keep the velocity running and setting another temperature. And I don't know how to do this.
I hope I could make my problem clear somehow and it is not too confused.
Typically a process can be seen a stack of function/procedure/method calls. At any point in time your program will be in a single point of your code.
When we add multiple threads into the program, instead of a single stack, we now will have multiple stacks of function/procedure/method calls. Each of them will be at any point in time in a different points of your code.
In C/C++, your main thread (the one that started it all) will have at the bottom of the stack the int main(int argc,char**argv) function. That applies to both single threaded and the main thread of a multithreaded program.
How about the rest of the threads. Well, for each of them you will specify a starting function. Threads (and main threads) will start execution at the beginning of the start function and run til the end. The thread will be alive while its starting function is being executed.
Now thinking of your code. One of many possibilities is spawn a thread to execute your temperature or velocity functions. That would be your starting function for newly spawned thread. You can join() from the spawning thread to wait for it to complete.
The thing about threads vs. other multiprocessing ways of organizing code (e.g. heavy processes) is that although we've seen that each of the threads have its own call stack, they all share the same memory.
So, while is not possible to modify the arguments of the thread starting function (that train has already passed)... However, other parts of the code could simply change some value in the memory (which is shared) and the starting function of the thread could be periodically checking that memory to detect the change and modify its behaviour.
But that brings a different problem: accessing/reading/writing shared memory potentially from multiple threads can lead to unpredictable results. So (any kind of) access must be protected with some sort of synchronization (mutexes, atomics...)
I have a classic physics-thread vs. graphics-thread problem:
Say I'm running one thread for the physics update and one thread for rendering.
In the physics thread (pseudo-code):
while(true)
{
foreach object in simulation
SomeComplicatedPhysicsIntegration( &object->modelviewmatrix);
//modelviewmatrix is a vector of 16 floats (ie. a 4x4 matrix)
}
and in the graphics thread:
while(true)
{
foreach object in simulation
RenderObject(object->modelviewmatrix);
}
Now in theory this would not require locks, as one thread is only writing to the matrices and another is only reading, and I don't care about stale data so much.
The problem that updating the matrix is not an atomic operation and sometimes the graphics thread will read only partially updated matrices (ie. not all 16 floats have been copied, only part of them) which means part of the matrix is from one physics frame and part is from the previous frame, which in turn means the matrix is nolonger affine (ie. it's basically corrupted).
Is there any good method of preventing this without using locks? I read about a possible implementation using double buffering, but I cannot imagine a way that would work without syncing the threads.
Edit: I guess what I'd really like to use is some sort of triple buffering like they use on graphic displays.. anyone know of a good presentation of the triple buffering algorithm?
Edit 2: Indeed using non-synced triple buffering is not a good ideea (as suggested in the answers below). The physics thread can run mutiple cycles eating a lot of CPU and stalling the graphics thread, computing frames that never even get rendered in the end.
I have opted for a simple double-buffered algorithm with a single lock, where the physics thread only computes as much as 1 frame in advance of the graphics thread before swapping buffers. Something like this:
Physics:
while(true)
{
foreach physicstimestep
foreach object in simulation
SomeComplicatedPhysicsIntegration( &object->modelviewmatrix.WriteBuffer);
LockSemaphore()
SwapBuffers()
UnlockSemaphore()
}
Graphics:
while(true)
{
LockSemaphore()
foreach object in simulation
RenderObject(object->modelviewmatrix.ReadBuffer);
UnlockSemaphore()
}
How does that sound?
You could maintain a shared queue between the two threads, and implement the physics thread such that it only adds a matrix to the queue after it has fully populated all of the values in that matrix. This assumes that the physics thread allocates a new matrix on each iteration (or more specifically that the matrices are treated as read-only once they are placed in the queue).
So any time your graphics thread pulls a matrix out of the queue, it is guaranteed to be fully populated and a valid representation of the simulation state at the time at which the matrix was generated.
Note that the graphics thread will need to be able to handle cases in which the queue is empty for one or more iterations, and that it would probably be a good idea to world-timestamp each queue entry so that you have a mechanism of keeping the two threads reasonably in sync without using any formal synchronization techniques (for instance, by not allowing the graphics thread to consume any matrices that have a timestamp that is in the future, and by allowing it to skip ahead in the queue if the next matrix is from too far in the past). Also note that whatever queue you use must be implemented such that it will not explode if the physics thread tries to add something at the same time that the graphics thread is removing something.
but I cannot imagine a way that would work without syncing the threads.
No matter what kind of scheme you are using, synchronizing the threads is an absolute essential here. Without synchronization you run the risk that your physics thread will race far ahead of the graphics thread, or vice versa. Your program, typically a master thread that advances time, needs to be in control of thread operations, not the threading mechanism.
Double buffering is one scheme that lets your physics and graphics threads run in parallel (for example, you have a multi-CPU or multi-core machine). The physics thread operates on one buffer while the graphics thread operates on the other. Note that this induces a lag in the graphics, which may or may not be an issue.
The basic gist behind double buffering is that you duplicate your data to be rendered on screen.
If you run with some sort of locking, then your simulation thread will always be rendering exactly one frame ahead of the display thread. Every piece of data that gets simulated gets rendered. (The synchronization doesn't have to be very heavy: a simple condition variable can frequently be updated and wake the rendering thread pretty cheaply.)
If you run without synchronization, your simulation thread might simulate events that never get rendered, if the rendering thread cannot keep up. If you include a monotonically increasing generation number in your data (update it after each complete simulation cycle), then your rendering thread can simply busy-wait on the two generation numbers (one for each buffer of data).
Once one (or both) of the generation numbers is greater than the most-recently-rendered generation, copy the newest buffer into the rendering thread, update the most-recently-rendered counter, and start rendering. When it's done, return to busy waiting.
If your rendering thread is too fast, you may chew through a lot of processor in that busy wait. So this only makes sense if you expect to periodically skip rendering some data and almost never need to wait for more simulation.
Don't update the matrix in the physics thread?
Take a chunk, (perhaps a row you have just rendered), and queue its position/size/whatever to the physics thread. Invert/transpose/whateverCleverMatrixStuff the row of modelviewmatrix's into another, new row. Post it back to the render thread. Copy the new row in at some suitable time in your rendering. Perhaps you do not need to copy it in - maybe you can just swap out an 'old' vector for the new one and free the old one?
Is this possible, or is the structure/manipulation of your matrices/whatever too complex for this?
All kinda depends on the structure of your data, so this solution may well be inappropriate/impossible.
Rgds,
Martin
Now in theory this would not require locks, as one thread is only writing to the matrices and another is only reading, and I don't care about stale data so much.
Beware: without proper synchronization, there is no guarantee that the reading thread will ever observe any changes by the writing thread. This aspect is known as visibility, and sadly, it is often overlooked.
Assuming lockless or near-lockless updates is actually what would solve your problem best, it sounds like you want the physics thread to calculate a new matrix, and then instantaneously update all those values at once, so it doesn't matter what version of the matrices the graphics thread gets, as long as (a) it gets them eventually and (b) it never gets half of one and half of the old one.
In which case, it sounds like you want a physics thread something like:
/* pseudocode */
while (true) foreach (object in simulation) {
auto new_object = object;
SomeComplicatedPhysicsIntegrationInPlace(new_object)
atomic_swap(object, new_object); // In pseudocode, ignore return value since nowhere
// else changes value of object. In code, use assert, etc
}
Alternativelty, you can calculate a new state of the whole simulation, and then swap the the values. An easy way of implementing this would be:
/* Psudocode */
while (true) {
simulation[ 1-global_active_idx ] = simulation[ global_active_idx ];
foreach (object in simulation[global_inactive_idx]) {
SomeComplicatedPhysicsIntegrationInPlace(object);
}
global_active_idx = 1-global_active_idx; // implicitly assume this is atomic
}
The graphics thread should constantly render simulation[global_active_idx].
In fact, this doesn't work. It will work in many situations because typically, writing a 1 to memory location containing 0 is in fact atomic on most processors, but it's not guaraneed to work. Specifically, the other thread may never reread that value. Many people bodge this by declaring the variable volatile, while works on many compilers, but is not guaranteed to work.
However, to make either example work, all you need is an atomic write instruction, while C++ doesn't provide until C++0x, but is fairly easy for compilers to implement, since most "write an int" instructions ARE atomic, the compiler just has to ensure that.
So, you can write your code using an atomic_swap function at the end of the physics loop, and implement that in terms of (a) a lock, write, unlock sequence -- which shouldn't block the graphics thread significantly, beause it's only blocking for the length of time of one memory write, and possibly only doing so once per whole frame or (b) a compiler's built in atomic support, eg. http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html
There are similar solutions, eg. the physics thread updating a semaphore, which the graphics thread treats simply as a variable with value 0 or 1; eg. the physics thread posts finished calculations to a queue (which is implemented internally in a similar way to the above), and the graphics thread constantly renders the top of the queue, repeating the last one if the queue underflows.
However, I'm not sure I'm understanding your problem. Why is there any point updating the graphics if the physics hasn't changed? Why is there any point updating the physics faster than the physics, can't it extrapolate further in each chunk? Does locking the update actually make any difference?
You can make two matrices fit in one cache line size (128 bytes) and aligned it to 128 bytes. This way the matrix is loaded in a cache line and therefore write to and from memory in one block. This is not for the feint of heart though and will require much more work. (This only fixes the problem of reading a matrix while it is being updated and getting a non-orthogonal result.)
This must be an easy question but I can't find a properly answer to it.
I'm coding on VS-C++. I've a custom class 'Person' with attribute 'height'. I want to call class method Grow() that starts a timer that will increment 'height' attribute every 0.5 seconds.
I'll have a StopGrow() that stops the timer and Shrink() that decrements instead of increment.
I really need a little push on which timer to use and how to use it within Grow() method. Other methods must be straight forward after knowing that.
That's my first question here so please be kind (and warn me if I'm doing it wrong :) Forgive my English, not my first language.
Do you really need to call the code every half second to recalculate a value? For most scenarios, there is another much simpler, faster, effective way.
Don't expose a height member, but use a method such as GetHeight(), which will calculate the height at the exact moment you need it.
Your Grow() method would set a base height value and start time and nothing else. Then, your GetHeight() method would subtract the starting time from the current time to calculate the height "right now", when you need it.
No timers needed!
Since you're on Windows, the simplest solution is probably to use the GetTickCount() function supplied by Windows.
There isn't a good timer function in the C++ language with a precision guaranteed to be less than a second.
So instead, include the windows.h header, and then call GetTickCount() to get a number of milliseconds. The next time you call it, you simlpy subtract the two values, and if the result is over 500, half a second has elapsed.
Alternatively, if you want to block the thread for half a second, use the Sleep(n) function, where n is the number of milliseconds you want the thread to sleep. (500 in your case)
You might want to take a look at CreateTimerQueue() and CreateTimerQueueTimer(). I've never personally used them, but they would probably fit the bill.
I currently spawn a thread that is responsible for doing timer based operations. It calls WaitForSingleObject() on a manual-reset event with a 10ms timeout. It keeps an internal collection of callbacks in the form of pointer-to-method and objects that the callbacks are invoked for. This is all hidden behind a singleton that provides a scheduler interface that let's the caller schedule method calls on the objects either after a timer expiration or regularly on an interval. It looks like the two functions that I mentioned should give you pretty much the same functionality... hmmm... might be time to revisit that scheduler code... ;-)
Sleep() an the normal timer event run off a 10ms clock.
For high resolution timer events on windows use high resolution timers
Not an easy question at all! You have at least two possibilities:
create a thread that will execute a loop: sleep 0.5s, increase height, sleep 0.5s, increase height, etc.
invert flow of control and pass it to some framework like Boost::Asio that will call your timer handler in every 0.5s.
In order to make the right decision you have to think about your whole application. Does it compute something (then maybe threads)? Does it interact with the user (then maybe event driven)? Each approach has some gotchas:
When you use threads you have to deal with locking, which can be tricky.
When you do event-driven stuff, you have to write asynchronous handlers, which can be tricky.