diff --git a/libs/main/io_devices/piethernet.cpp b/libs/main/io_devices/piethernet.cpp index d4d7d04e..d91fe792 100644 --- a/libs/main/io_devices/piethernet.cpp +++ b/libs/main/io_devices/piethernet.cpp @@ -139,6 +139,7 @@ PIEthernet::PIEthernet(int sock_, PIString ip_port): PIIODevice("", ReadWrite) { addr_s.set(ip_port); sock = sock_; opened_ = connected_ = true; + is_server_client = true; init(); setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop); setType(TCP_Client, false); @@ -168,7 +169,7 @@ void PIEthernet::construct() { setTTL(64); setMulticastTTL(1); server_thread_.setData(this); - server_thread_.setName("__S__server_thread"_a); + server_thread_.setName("_S.tcpserver"_a); #ifdef MICRO_PIP setThreadedReadBufferSize(512); #else @@ -179,7 +180,7 @@ void PIEthernet::construct() { bool PIEthernet::init() { - if (isOpened()) return true; + if (isOpened() || is_server_client) return true; if (sock != -1) return true; // piCout << "init " << type(); PRIVATE->event.destroy(); diff --git a/libs/main/io_devices/piethernet.h b/libs/main/io_devices/piethernet.h index e2a2e5e9..3015fb3d 100644 --- a/libs/main/io_devices/piethernet.h +++ b/libs/main/io_devices/piethernet.h @@ -483,6 +483,7 @@ protected: PRIVATE_DECLARATION(PIP_EXPORT) int sock, sock_s; std::atomic_bool connected_, connecting_, listen_threaded, server_bounded; + bool is_server_client = false; mutable PINetworkAddress addr_r, addr_s, addr_lr; Type eth_type; PIThread server_thread_; diff --git a/libs/main/thread/pithread.cpp b/libs/main/thread/pithread.cpp index 5dd1f287..c1810e68 100644 --- a/libs/main/thread/pithread.cpp +++ b/libs/main/thread/pithread.cpp @@ -919,6 +919,10 @@ void PIThread::_runThread() { void PIThread::_endThread() { + PIScopeExitCall ec([this] { + terminating = running_ = false; + tid_ = -1; + }); // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "..."; stopped(); // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "ok"; @@ -927,8 +931,6 @@ void PIThread::_endThread() { end(); // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "ok"; if (lockRun) thread_mutex.unlock(); - terminating = running_ = false; - tid_ = -1; // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "exit"; // cout << "thread " << t << " exiting ... " << endl; // PICout(PICoutManipulators::DefaultControls) << "pthread_exit" << (__privateinitializer__.p)->thread; @@ -943,7 +945,7 @@ void PIThread::_endThread() { #elif defined(FREERTOS) PRIVATE->thread = 0; #else - pthread_detach(PRIVATE->thread); + // pthread_detach(PRIVATE->thread); PRIVATE->thread = 0; pthread_exit(0); #endif