[try Beta version]
Not logged in

 
A Question About Threads

Apr 9, 2016 at 3:24am
How could someone make two threads that can operate in the background without disrupting the flow of a program. I am trying to do this for an IRC Client but I can't get it right.
Apr 9, 2016 at 5:29am
Apr 9, 2016 at 11:54am
I tried using regular threads they seem not to work.
Apr 9, 2016 at 1:23pm
"They seem not work" is pretty vague. It's going to be very difficult to help you with just that information.

What happened when you did this?
std::thread first (function_to_run_in_thread);
Apr 10, 2016 at 3:20am
This is a the program in question and I expect no one to read it.
https://drive.google.com/file/d/0B4fqFvNG5t5oclMwLVM5WWhiejg/view?pref=2&pli=1

But this is an exemplar of the problem I am talking about.
1
2
3
4
5
6
7
8
9
10
11
void loop()
{
while(true){}
}

void main()
{
start loopThread1(loop);
start loopThread2(loop);
// program cannot halt after the start of threads
}
Apr 10, 2016 at 3:38am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <atomic>
#include <thread>

std::atomic<bool> exiting(false);

void loop()
{
    while (!exiting)
        ;
}

int main()
{
    std::thread t1(loop);
    std::thread t2(loop);

    exiting = true;

    t1.join();
    t2.join();
}
Topic archived. No new replies allowed.