如何告诉父级线程是使用pthreads在C ++中完成的? [英] How to tell the parent that the thread is done in C++ using pthreads?

查看:150
本文介绍了如何告诉父级线程是使用pthreads在C ++中完成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TCP服务器应用程序,使用POSIX线程和C ++在一个新线程中为每个客户端提供服务。

I have a TCP Server application that serves each client in a new thread using POSIX Threads and C++.

服务器在其套接字上调用listen,当客户端连接时,它创建一个类为Client的新对象。

The server calls "listen" on its socket and when a client connects, it makes a new object of class Client. The new object runs in its own thread and processes the client's requests.

当客户端断开连接时,我想要一些方法告诉我的main()线程这个线程已经完成了,main()可以删除这个对象,并记录类似客户端断开连接。

When a client disconnects, i want some way to tell my main() thread that this thread is done, and main() can delete this object and log something like "Client disconnected".

我的问题是,我如何告诉主线程, done?

My question is, how do i tell to the main thread, that a thread is done ?

推荐答案

pthreads return 0 如果一切顺利 errno 如果某些操作不起作用。

pthreads return 0 if everything went okay or they return errno if something didn't work.

int ret, joined;
ret = pthread_create(&thread, NULL, connect, (void*) args);
joined = pthread_join(&thread, NULL);

如果加入为零,完成。清理线程的对象。

If joined is zero, the thread is done. Clean up that thread's object.

这篇关于如何告诉父级线程是使用pthreads在C ++中完成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆