dimanche 26 juillet 2015

Trying to understand threading in C++

I am a C# programmer and i need to create a program in c++ now. What i'm trying to achieve is make an infinite thread and give it a sleep time.

In c# i could easily do this like

in main function

new Thread(taskfunction).Start();

and taskfunction

    private void taskfunction()
    {
    while(true){Thread.Sleep(2500); // do stuff}
    }

so this would work in the background as long as my program is on. I am trying to achieve the same thing in c++ like:

in main function

std::thread somethread(taskfunction);
somethread.join();

and taskfunction

void taskfunction()
{
while(true){this_thread::sleep_for(chrono::milliseconds(2500));// Do Stuff}
}

So, while c# thread starts and moves on, c++ thread waits on somethread.join();. What is the difference and How can i achieve what i do in c# also in c++?

Thank you

Aucun commentaire:

Enregistrer un commentaire