socket + pthread 資源佔用情形解決
在使用 pthread_create 建立 thread 處理 socket 的時候要注意,縱使 socket close 了,資源還是會被 thread 佔用住,此時要採用分離式 thread,才能在 thread 結束時自動釋放資源,最簡單的方式是建立 thread 後設定為 detach。
while (1)
{
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
exit(1);
pthread_t thread1;
pthread_create(&thread1, NULL, doSocketThread, &newsockfd );
pthread_detach(thread1);
}
訂閱:
張貼留言 (Atom)
更高效處理 micro second 的方式
更高效處理 micro second 的方式… 以 STM32 為例… __IO unsigned long sys_tick = 0; void SysTick_Handler(void) { HAL_IncTick(); sys_tick += (SysTi...
-
rails blog -d mysql 建立一個名為 blog 的網站app,使用 mysql cd blog 進去 gen 好的網站 config/database.yml 編輯資料庫設定 rake db:create 自動建立資料庫 ruby script/generate ...
-
https://github.com/ljean/modbus-tk/ install pip… sudo apt-get install python-pip install... download modbus_tk-x.x.x.tar.gz tar zxvf...
-
DMX protocol… # 1:break + 1:start + 512:channel python: import serial class DMX512: def __init__(self, port): self.seria...
沒有留言:
張貼留言