#Writing a multithreaded application to check for variables while other parts of the code are running

18 messages · Page 1 of 1 (latest)

tulip crest
#

I am working on a project and we have concluded that we will need to use multithreading(asked on this server earlier). I am unsure on how to use multithreading. I want to able to have a thread to be constantly reading a variable and the other thread to take the variable and run code based on it.

fading swallowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

rotund goblet
#

Look into atomic types if you're wanting to share an integral type

#

And some type of mutex if you're wanting to share a class

tulip crest
#

okay. I will wee what I can find

#

How do I create a thread?

tulip crest
rotund goblet
#

I'm not looking at a massive file that you dump on the server go look stuff up

tulip crest
#

I think moving if(bigPacket)... out of the void loop would help

tawdry kiln
# tulip crest How do I create a thread?
void what_you_want_to_do()
{
    std::cout << "Do soemthing in a different thread";
}
int main()
{

    auto t = std::thread(&what_you_want_to_do);
    t.join();

}``` << like this, orrr use `std::jthread`
tulip crest
tawdry kiln
# tulip crest can you add a whole bunch of code in the "Do something in a different thread"

yeah as much as you want, It also doesn't have to be a whole other function it can be a lambda too and you can pass in variables too like this... ```cpp
void what_you_want_to_do()
{
std::cout << "Do soemthing in a different thread\n";
}

void what_you_want_to_do_2(int x, int y)
{
std::cout << x << y << std::endl;
}

int main()
{

auto t = std::thread(&what_you_want_to_do);
auto t1 = std::thread(&what_you_want_to_do_2, 10, 20);
auto t2 = std::thread([]() {
    std::cout << "yet another thread\n";
    });
t.join();
t1.join();
t2.join();

}

royal granite
lyric cosmos
#

This

#

If you are only Reading from multiple thread you dont need mutex or atomic

#

Only if u write

tulip crest
#

Okay

#

!solved