08-12-2009, 06:29 PM
Hi. I wanted to know what is the substitute for Threads in c++. Or is there any?
We can use threads to run simulatinous parts of a program parallely in java... so how can we do that in c++. a sample program or a hint would help.
Thanks in advance
08-12-2009, 07:10 PM
They are called threads in C++ too. Threads isn't a language specific term.
As for creating them, it depends on the platform, different platforms have different APIs.
For Windows, I believe the function is createthread().
A trivial example is the ubiquitous "searching through an array" problem, which can become faster with proper parallel programming. Say for instance you have an array of 10 elements and you want to search for a specific value in the array. Say that you want to create one thread per core (though, I think some cores can handle two threads).
So, in a dual-core system, you could divide the array in two, so that one thread searches from elements 0 - 4 while the other searches from elements 5 - 9.
When you are parallel programming there are a lot of other things to consider, for instance variable usage. Most processors implement a mechanism to lock a variable from write/read access (either that, or you have to implement it yourself) while another core is performing logic on it - so other cores will not receive a possibly outdated value to perform it's own logic on. If one core is performing logic on the same variable, the other core is going to have to wait until the first core finishes. This idle time can actually increase your program's execution time drastically rather than diminish it.
On the flip side, it is possible that there is no locking mechanism, and cores work with outdated values.
Nowadays processors are very smart. Even if you program sequentially, the processor will break distinct tasks amongst the cores to increase program performance, so there is a chance that you don't even have to parallel program to reap the benefits.
I suggest picking up a Graduate Level book on parallel programming if you want to get really good at it.
08-14-2009, 05:12 PM
i want this only to test my codes of OpenGl using parallel programing to see the % increase in performance v/s time. But dont want to go in detail... I searched the net and found that there was too big explanations.
Can any one of u just write a sample code where in i need to just call it. If its quite easy to implement it. I am using VS2003 .net. And if not can some one point me to a place where Threads are explained clearly.
Thanks in advance
08-14-2009, 05:16 PM
Its not that simple, once you try to multi-thread a process you need to place in appropriate protections into your code. Shared resources between the threads need to be protected with mutexes or semaphores. And you have to make sure that you don't create deadlocks.