Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
2.80/5 (3 votes)
Hello,

I have an event handler in the main thread, which receive events from outside process. And upon the receipt of a new event, a new thread is created to handle the event. The problem is whenever I create a new thread inside the event handler, it will be destroyed when the event handler finishes. To solve this problem, after the creation of the thread, I am calling its join function, but this will cause the main thread which has the event handler to block its execution until the thread finishes its job. And actually this will be converted back to a single thread case. Where for each event, a new thread is created when the previous, thread is destroyed.

For more explanation ,please check the code below:
C#
void ho_commit_indication_handler(message &msg, const boost::system::error_code &ec)
{
.....
}

void event_handler(message &msg, const boost::system::error_code &ec)
{
    if (ec)
    {
        log_(0, __FUNCTION__, " error: ", ec.message());
        return;
    }

    switch (msg.mid())
    {
        case n2n_ho_commit:
        {
            boost::thread thrd(&ho_commit_indication_handler, boost::ref(msg), boost::ref(ec));
            thrd.join();
        }
        break
    }
};

So my question is, how to handle each event through a separate thread and keep the thread alive even if the main thread exits the event_handler?

Note: I am using Boost 1.49 library


If what you're saying is correct. So why this happens in the following example:

C++
#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <chrono>

void foo()
{
    std::chrono::milliseconds dura( 2000 );
    std::this_thread::sleep_for( dura );
    std::cout << "Waited for 2Sec\n";
}
 
void bar(int x)
{
    std::chrono::milliseconds dura( 4000 );
    std::this_thread::sleep_for( dura );
    std::cout << "Waited for 4Sec\n";
}
 
int main()
{
  std::thread first (foo);
  std::thread second (bar,0);
 
  return 0;
}


You're saying the thread is alive. I get the following output:
terminate called without an active exception
Aborted (core dumped)

This happened because the main thread has terminated, before the thread terminations

Thanks a lot.
Posted
Updated 16-Mar-14 7:58am
v5
Comments
Sergey Alexandrovich Kryukov 15-Mar-14 22:08pm    
If you is Boost, you are not using C++/CLI, you are using C++.
—SA
Mr.HA-AS 15-Mar-14 22:15pm    
OK, I modified it.
Sergey Alexandrovich Kryukov 15-Mar-14 22:17pm    
Good. And in the meanwhile, I answered.
—SA
Sergey Alexandrovich Kryukov 15-Mar-14 22:18pm    
There is no issue. The code simply makes no sense. Perhaps I could advise what to do if you explain your ultimate purpose.
But I would rather advise to learn the threading in general...
—SA

1 solution

The question makes no sense: the threads are always executed outside the function which created them. More exactly, threads and functions are orthogonal: any function can be executed be several thread, each thread can call several functions (as in single-threaded programming). The structure of the calls/returns, function parameters and local variables are based on the stack; and each thread has its own separate thread.

Now, let's see what you are doing. You create some thread and then join the same thread. It means that the calling thread, the one which called the function join is put in the wait state, that is, switched of and not scheduled back to execution until it is waken up. One of the events to wake up the working thread is completion of the thread you are trying to join. In other words, calling thread is sleeping, another thread is being executed to the very end, and than calling thread resumes the execution. It simply means that the two threads work one after another, never do anything in parallel. In turn, it means that you totally defeat the purpose of threading.

I don't want to answer "How to keep threads working outside the the function which created them". This is not what you want. I may only think that you want it, but in fact, this question does not make any sense at all. What you really want is to understand what are threads, their purpose and usage. Right now, you don't have a clue, not even close. I would advise to learn about it; this is very important.

—SA
 
Share this answer
 
v2
Comments
Mr.HA-AS 15-Mar-14 22:33pm    
I know about the threads, but I believe is my problem is as following:
Consider the previous code without having the join function being executed after the creation of the thread. The newly created thread will start to work in parallel with the function that created it. But when the event_handler reaches the end, it will call the default destructor of the newly created thread which is working in parallel.

And this is what actually happened with me, the program crashed when I did this.

So my question is how to keep the thread alive? what I think is like to have a thread pool outside the scope of the function, am I correct or not?
Sergey Alexandrovich Kryukov 15-Mar-14 23:03pm    
Maybe "knowing about the threads" is not enough. You don't need to care about crashing, because the whole idea to start thread and join it makes no purpose. The question is not how to keep thread alive. It is alive. When join call is returned, it happens then the thread is complete, which is defined by the thread method. There is nothing to "keep alive".
—SA
Mr.HA-AS 16-Mar-14 10:23am    
Check, what I posted now please.
Sergey Alexandrovich Kryukov 16-Mar-14 12:15pm    
This "code" is even worse. I repeat: you need to learn threading. You did not explain your ultimate goals and do not understand threading, you are not digesting my help.

But okay, you formally accepted you own non-solution. So, no more help for you, as you are not playing nice. You think you got the solution, be it your way then.

—SA
Mr.HA-AS 16-Mar-14 13:57pm    
You're not getting what I want. Other threads were more helpful. And explained to me what I wanted.
The solution is to use detach() command. And you should keep the main thread not matter what.
So thanks for being a nice and helpful person.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900