1

I have problem with ending detached thread:

void fun_5(int sock_connect)    
{
    unsigned char buff2[76] = {0x01 ,0x01 ,0x02 ,0x02 ,0x00 ,0x4a ,0x31 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x04 ,0x00 ,0x00 ,0x50 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x50 ,0x4c ,0x4d ,0x4f ,0x4d ,0x49 ,0x46 ,0x41 ,0x58 ,0x20 ,0x20 ,0x4e ,0x52 ,0x20 ,0x41 ,0x00 ,0x34 ,0x41 ,0x50 ,0x3c ,0x32 ,0x31 ,0x3e ,0x35 ,0x46 ,0x2e ,0x52 ,0x32 ,0x31 ,0x30 ,0x2f ,0x31 ,0x35 ,0x2f ,0x31 ,0x32 ,0x31 ,0x35 ,0x3a ,0x31 ,0x31 ,0x3a ,0x30 ,0x36 ,0x00 ,0xff ,0xef};
while(thfive)
    {   
            if((send( sock_connect, buff2, sizeof(buff2), MSG_NOSIGNAL ) ) <= 0 )
            {
                close(sock_connect);
                break;
            }
            sleep(5);
    }

}       

This thread sends bytes in every 5 sec when client which connect to server(my program) sends specified sequence of bytes("start"), and it should stop sending when client sends other sequence("stop").

while(thfive) <<- thfive is true when client sends "start", and its false when client sends "stop"

But when client sends "start" after "stop" in less then 5 sec, thread running all the time. Because of that this solution is not the best. There is option to checking condition even when thread sleep? Or how I can kill this detached thread from main thread?

It works in detached thread, because in the same time, program doing other functions. I have more threads like this in the same time ( which sends bytes every 1.5 sec, 10 sec etc). I'm still a novice in programming, and the first time I use multitasking.

Thanks.

Edit 1: My OS is Linux.

DarthBoa
  • 113
  • 2
  • 6
  • Instead of using `sleep` for the whole timeout, use some other function (like e.g. `select` with `NULL` descriptor sets) that can sleep for shorter periods of time, and call in a loop that checks the status of `thfive`. – Some programmer dude Sep 18 '14 at 08:36
  • But even with the solution in my previous comment, you still will have the possibility that the client sends "start" while the thread is sleeping, no matter how short the sleep interval is. All you can really do is minimize the risk, or simply not start a new thread if the old one is still running. – Some programmer dude Sep 18 '14 at 08:40
  • How is thfive declared? – Werner Henze Sep 18 '14 at 08:40
  • It is declared as boolean, and it changes depending on the incoming sequence of bytes – DarthBoa Sep 18 '14 at 08:48
  • did you check that the sequence of bytes that set `thfive` to false is ever received? – Jepessen Sep 18 '14 at 09:00
  • Yes it is, if my client sends starts and stops with more then 5 sec delay all work fine. – DarthBoa Sep 18 '14 at 09:07

1 Answers1

-1

When you let the thread run while thfive is true, you must take care. The compiler might optimize the loop so that it only checks thfive at the entry of the loop. thfive does not change inside the loop, so the compiler might think it is not necessary to check thfive every time the loop continues.

Starting with C++11 you should use std::atomic<bool> as type for thfive, see also this SO question.

You might also consider creating an event and signal that event if the worker thread shall terminate. But how to do this depends on the platform you are using. For Windows for example also the socket API can be used event based, so the thread could use one function call to wait for the terminate event or a socket event in parallel (no need to poll).

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
  • 2
    `volatile` is NOT suitable for this. Again: `volatile` is NOT suitable for creating synchronization-ish variables. You need atomics in any case here, since otherwise it's a data race and, in result, undefined behavior. [c++] suggests OP is using C++, and the current C++ standard is de facto C++14, so atomics are available. – Griwes Sep 18 '14 at 08:53
  • 1
    @Griwes I changed my suggestion to atomic, but also added volatile sig_atomic_t. Yet I disagree about assuming C++14. – Werner Henze Sep 18 '14 at 09:06
  • @WernerHenze Even in pre-C++11 land, using a `volatile bool` might easily break once you are running on an architecture with a weaker default memory ordering than x86. – ComicSansMS Sep 18 '14 at 09:12
  • @WernerHenze, the question is tagged "c++"; common sense says that "C++" means the latest released standard. If OP wanted pre-C++11 solutions, he would've marked the question with [tag:c++03]. – Griwes Sep 18 '14 at 09:22