Threads

Moin zusammen!


In der Konsole wird nur "foo" ausgegeben.
Wieso wird beides nicht "parallel" ausgegeben? (Hab ein Mehrkernprozessor)



Danke



Code:


#include <iostream>
#include <thread>
void foo() {
while(1==1)
std::cout << "foo" << std::endl;
}
void goo() {
while(1==1)
std::cout << "goo" << std::endl;
}
int main() {
std::thread t1(foo);
t1.join();
std::thread t2(goo);
t2.join();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <thread>

void foo() {
    while(1==1)  // loop forever
        std::cout << "foo" << std::endl;
}

void goo() {
    while(1==1) // loop forever
        std::cout << "goo" << std::endl;
}

int main() {
    std::thread t1(foo);  // start thread that prints "foo" forever
    t1.join();  // wait for thread to complete, it never does
    std::thread t2(goo);
    t2.join();
}
#include <iostream>
#include <thread>

void foo() {
while(1==1) // loop forever
std::cout << "foo" << std::endl;
}

void goo() {
while(1==1) // loop forever
std::cout << "goo" << std::endl;
}

int main() {
std::thread t1(foo); // start thread that prints "foo" forever
t1.join(); // wait for thread to complete, it never does
std::thread t2(goo);
t2.join();
}

How can i start thread 2?
To get a multi threading program?
How can i start thread 2?

By allowing your program to continue to the line that starts it. Currently, you're preventing that from happening by telling your program to wait for a thread to finish, when that thread can never finish.

Do you understand why this is happening?

EDIT: Also, please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on


By allowing your program to continue to the line that starts it. Currently, you're preventing that from happening by telling your program to wait for a thread to finish, when that thread can never finish.

Do you understand why this is happening?



I understand the idea behind it

My goal is that the first thread writes data from buffer to hard disk (every second).

The second thread should write new data into the buffer.
Last edited on
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
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>

namespace { std::mutex cout_mutex ; }

[[noreturn]] void infinite_loop( const char* msg, unsigned int interval_milli_secs ) {

    const auto wait_time = std::chrono::milliseconds(interval_milli_secs) ;

    unsigned long long n = 0 ;

    while(true) { // loop forever

        {
            std::lock_guard<std::mutex> guard_cout(cout_mutex) ;
            std::cout << ++n << ". " << msg << '\n' << std::flush ;
        }

        std::this_thread::sleep_for(wait_time) ;
    }
}

[[noreturn]] void foo() { infinite_loop( "a: foo", 500 ) ; }
[[noreturn]] void goo() { infinite_loop( "b: goo", 700 ) ; }

int main() {

    std::cout << "*** press enter to quit program *** \n\n" << std::flush ;

    // start two threads
    std::thread foo_thread(foo) ;
    std::thread goo_thread(goo) ;

    // wait for user to press enter
    std::cin.get() ;

    // detach from the threads and quit gracefully
    // https://en.cppreference.com/w/cpp/thread/thread/detach
    foo_thread.detach() ;
    goo_thread.detach() ;

    std::cout << "bye!\n" << std::flush ;
}
Thanks
Topic archived. No new replies allowed.