[try Beta version]
Not logged in

 
multi threading

Jun 12, 2013 at 9:13am
Hi. I'm working on c++ for a year. I wanna know if it is possible in c++ to run two (or more) function in a same time.
For example one function prints number in a infinite loop and the other one waits to catch an input. If the input was character 'b' (for example) then the loop stop printing.
Can you help me to make a program like this?
Jun 12, 2013 at 10:21am
Sure, something like that could be pretty simple:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <thread>
#include <chrono>
#include <future>
#include <iostream>

int main()
{
    std::cout << "Enter b to quit\n";

    std::promise<void> finish_p;
    std::future<void> finish_f(finish_p.get_future());

    // launch thread
    std::thread t([&]{
        do { 
            std::cout << "number" << std::endl; // keep printing this every second
        } while(finish_f.wait_for(std::chrono::seconds(1)) != std::future_status::ready);
    });
    // end of the thread body

    // wait for input
    for(char c; std::cin.get(c) && c != 'b'; )
    {   
        // not 'b', wait for more input
    }

    // signal the thread to exit
    finish_p.set_value();

    // wait for the thread to finish
    t.join();
}

Jun 14, 2013 at 9:53am
My compiler doesn't find thread, chrono and future libraries.
Jun 14, 2013 at 7:19pm
You have to do multithreading for software graphics processing because rendering pixel by pixel is too slow when you have to handle game logic and such.

And you might be using an old compiler or ide suite. <thread>,... are C+11 standard.
Last edited on Jun 14, 2013 at 7:20pm
Jun 15, 2013 at 11:52am
C++11 offers a native support for multithreading
there are also other libs such as boost::thread or pthread that work on older versions of the standard too.
Jun 15, 2013 at 12:43pm
Is there any book that has explane all these new libraries?
I've read Deitel & Dietel fourth edition and it doesn't include any of them.
Jun 15, 2013 at 12:47pm
Jun 15, 2013 at 3:20pm
Yeah, he is definitly using an older compiler (even if only a little).
<thread> is one of the awesome C++11 features (too bad C++11 isn't fully supported yet)!

It's really awesome, but sadly I (like most), don't have a lot of C++11 compatible compilers...
I always wanted <thead> and that cool sleep_for functionality!!!

sleep_for is revolutionary!
Jun 15, 2013 at 3:51pm
sleep_for is revolutionary!

It was available from boost.thread for many years.
Jun 16, 2013 at 6:44am
I use minGW 3.2.0 compiler for my windows and fedora13's default gcc compiler .
Jun 16, 2013 at 8:17am
Go to compiler settings flags then put -std=c++11
This will enable c++11 then you should be able to use #include <thread> and all the other c++11 features like range-based for loops and initialization

*http://www.cplusplus.com/reference/thread/thread/?kw=thread
Last edited on Jun 16, 2013 at 8:18am
Jun 16, 2013 at 11:50am
Go to compiler settings flags then put -std=c++11

... that's assuming the compiler supports C++11 at all, and supports GCC options.
Jun 16, 2013 at 4:25pm
He said he used mingw 3.2.0 which is GCC I thought but yeah I don't know if it supports c++11 or not I just figured most people have modern compilers besides all those people that use borland.
Jun 16, 2013 at 8:31pm
Most GCC users don't. Look at the compilers on stable versions RedHat, CentOS, Debian, OpenBSD, FreeBSD, ...
Jun 17, 2013 at 5:44pm
closed account (o1vk4iN6)
You have to do multithreading for software graphics processing because rendering pixel by pixel is too slow when you have to handle game logic and such.


Actually, rendering the pixels is probably the fastest part for that pipeline.
Jun 19, 2013 at 12:01am

Actually, rendering the pixels is probably the fastest part for that pipeline.


Not if you do it with gdi. And I think for DirectX and Opengl they create thread in the background automatically for handling/accessing graphics card RAM on initialization. Otherwise it will be way too slow at least for my computer to render/swap/copy multiple images in one main thread.
Last edited on Jun 19, 2013 at 12:01am
Jun 19, 2013 at 12:17am
closed account (o1vk4iN6)
Not if you do it with gdi. And I think for DirectX and Opengl they create thread in the background automatically for handling/accessing graphics card RAM on initialization. Otherwise it will be way too slow at least for my computer to render/swap/copy multiple images in one main thread.


Yes even with gdi, you have a finite number of pixels. There are a lot of techniques for graphics used to improve performance by simply filling in the pixels that are required, most recent being Euclideon's technique. If you are loading textures into GPU memory every time you draw something on the screen (30-60 fps) that is a fault with your implementation and not cause "rendering pixel by pixel is too slow".
Topic archived. No new replies allowed.