简体   繁体   English

C++ 结束与主线程分离的线程

[英]C++ Ending detached thread from main thread

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").当连接到服务器的客户端(我的程序)发送指定的字节序列(“开始”)时,该线程每 5 秒发送一次字节,当客户端发送其他序列(“停止”)时,它应该停止发送。

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.但是当客户端在不到 5 秒的时间内在“停止”之后发送“开始”时,线程一直在运行。 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).我同时有更多这样的线程(每 1.5 秒、10 秒等发送一次字节)。 I'm still a novice in programming, and the first time I use multitasking.我还是编程新手,第一次使用多任务。

Thanks.谢谢。

Edit 1: My OS is Linux.编辑 1:我的操作系统是 Linux。

When you let the thread run while thfive is true, you must take care.当你让线程运行而thfive为真时,你必须小心。 The compiler might optimize the loop so that it only checks thfive at the entry of the loop.编译器可能会优化循环,以便它只在循环入口处检查 thfive。 thfive does not change inside the loop, so the compiler might think it is not necessary to check thfive every time the loop continues. thfive 在循环内不会改变,因此编译器可能认为没有必要在每次循环继续时检查 thfive。

Starting with C++11 you should use std::atomic<bool> as type for thfive, see also this SO question .从 C++11 开始,您应该使用std::atomic<bool>作为 thfive 的类型,另请参阅此 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).例如,对于 Windows,套接字 API 也可以基于事件使用,因此线程可以使用一个函数调用来并行等待终止事件或套接字事件(无需轮询)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM