threads

hey i want to use threads to run 2 actions at the same time.
so i tried the way i was able to see on sites, but after compiling
my gui pops up and closing again...
so here would be my code :


#include "main.h"
#include "projekt.h"
#include "server.h"

#include <iostream>
#include <thread>
using namespace std;


IMPLEMENT_APP(MyApp)


bool MyApp::OnInit() {

std::thread first;
std::thread second ;

wxThread* test;

first = std::thread(&MyApp::i_face, this);
second = std::thread(&MyApp::serverM, this);

second.join();
first.join();


return true;


}
void MyApp::i_face() {

wxInitAllImageHandlers();
Projekt* projekt = new Projekt(wxT("\tUser Interface"));
projekt->Show(true);
}
void MyApp::serverM() {

server* a = new server() ;
}


i read some things about threads, but was not able to realize it..
hopefully someone is able to help.

Regards,
MrSY
Last edited on
> my gui pops up and closing again...
¿and what's the problem? both functions end, the program terminates
¿what do you expect to happen?
Last edited on
its useless, but a good way to see threads in an example is something like having both threads cout a different letter, say * and . chars that are very distinct. Have one of the thread sleep for say 100ms and the other for 500ms between iterations. Then watch the output. it should be a nice ...*...*...* looking output.
the problem is i want them to be done separately..
it means i want to use my interface while the server communicates with its client.
at the current state the thread affects my interface...
if i leave just 1 thread in use(second = std::thread(&MyApp::serverM, this)) and let it join after i call i_face() then the interface opens but is blank..
if i join it beforehand it stops the interface from opening.
it awaits it´s client to answer before it starts the interface..
hopefully i was able to explain my issue
Last edited on
Something along these lines, perhaps:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
#include <atomic>

struct MyApp
{
    void init() { OnInit() ; }
    void done()
    {
        quit = true ; // set the quit flag

        // wait for the threads to finish
        for( auto& thread : my_threads ) thread.join() ;

        std::cout << "done: all threads have finished\n" << std::flush ;
        my_threads.clear() ;
    }

    ~MyApp() { if( !my_threads.empty() ) done() ; }

    void OnInit()
    {
        // create the i_face and serverM threads and add them to the vector
        my_threads.push_back( std::thread( &MyApp::i_face, this ) ) ;
        my_threads.push_back( std::thread( &MyApp::serverM, this ) ) ;
    }

    void i_face()
    {
        while( !quit ) // keep running till quit flag is set
        {
            std::cout << "Myapp::i_face\n" << std::flush ;
            std::this_thread::sleep_for( std::chrono::milliseconds(500) ) ;
        }
    }

    void serverM()
    {
        while( !quit ) // keep running till quit flag is set
        {
            std::cout << "Myapp::serverM\n" << std::flush ;
            std::this_thread::sleep_for( std::chrono::milliseconds(800) ) ;
        }
    }

    std::vector< std::thread > my_threads ;
    std::atomic_bool quit = false ;
};

int main()
{
    MyApp this_app ;

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

        this_app.init() ;
        std::cin.get() ;
        this_app.done() ;
    }
}
Topic archived. No new replies allowed.