Hi C++ forums,
I am learning about various different things in c++ just to learn more. Then i came across an thing called Threads. I searched for it every where but when i look it seems all of it is not in c++ but in QT and c# and other like SFML networking (which i don't really want to learn). The thing is would come one here explain me what an thread is and how to use it or is it an simple function.Or better if you can just show me an small or simple code of an Thread and explain it to me please. Thanks in advance Cyberwarfare
Most simple programs (say a "Hello, World!" program) are fine to be executed linearly with one thread.
For example, in a Hello World program, your console will print the words and exit the program in that order. The program runs so fast, you don't see each letter being printed.
However, say you have a form and something in that background that requires a long calculation. If the calculation is run on the same thread as the form, the user could not move the form, press any buttons on the form, and in many cases, the form won't close unless it is forced to do so by the operating system.
If you wanted to use Boost you could do something similar to...
#include <boost/thread.hpp>
void formCallback()
{
// Maybe handle form-related stuff here...
}
void aCalculation()
{
// Perhaps something that takes a long time here...
}
int main ()
{
boost::thread threadOne = thread(formCallback);
boost::thread threadTwo = thread(aCalculation);
// Do stuff with your program
// Close when you are done
threadOne.join();
threadTwo.join();
return 0;
}
I'm sure the other members here can provide better code examples and also help if you want to use something other than Boost
Threading is part of the C++X11 standard. A thread is a set of instructions for a processor core to work on within a process. When a process has more then one thread, it means that it has more then one set of instructions to be worked on in parallel. This is done by the platform assigning a thread to a core, the number of threads that can be processed in parallel is limited to the number of cores that a processor has.
A thread is a set of instructions for a processor core to work on within a process. When a process has more then one thread, it means that it has more then one set of instructions to be worked on in parallel. This is done by the platform assigning a thread to a core, the number of threads that can be processed in parallel is limited to the number of cores that a processor has
This isn't entirely true. You can have threads running on multiple processors, not just multiple cores. Also threads are OS defined, and you aren't limited to the number of cores. Take linux for example, everything is a thread (no distinction between thread and process in linux). I have way more threads running than I do processor cores.
Threads are essentially the same on *nix as they are on Windows. Every process has at least one thread, when the last thread of a process finishes the process exits. There are multiple distinctions between processes and threads even in Linux. For example a process may have multiple threads but a thread may never contain a process, that's just not how memory allocation works. Threads are free to communicate with each other within the same process but in order to communicate with a thread in a different processes memory space the OS has to get involved even if that involvement is simply mapping a pointer to the target processes memory space.
A processor always has at least one core, the cores are what actually do the work. If a core tried to process multiple threads at once it wouldn't be able to keep track of what thread changed what register, or even what instruction to execute next. I am referring to logical cores by the way, not just physical ones. A logical core is a partition of a physical cores memory cache.
C++ doesnt know anything about Thread, but some other libraries, for example boost, have thread feature.
Simply saying, thread is a function(in c++) to be called in a particular time and the enviroment of it(registers, stack...). Your system switches the threads currently running very fast, so it seems like they are running at the same time.