第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

喚醒線程在accept()調(diào)用中被阻止

喚醒線程在accept()調(diào)用中被阻止

拉風(fēng)的咖菲貓 2019-12-03 16:14:57
Linux上的套接字問題我有一個(gè)工作線程被accept()調(diào)用阻塞。它只是等待傳入的網(wǎng)絡(luò)連接,進(jìn)行處理,然后返回以偵聽下一個(gè)連接。當(dāng)需要退出程序時(shí),如何發(fā)信號(hào)通知該網(wǎng)絡(luò)工作線程(從主線程)從accept()調(diào)用返回,同時(shí)仍然能夠正常退出其循環(huán)并處理其清理代碼。我嘗試過的一些事情:pthread_kill發(fā)送信號(hào)。感覺很笨拙,而且不能可靠地使線程執(zhí)行關(guān)閉邏輯。也使程序終止。我想盡可能避免發(fā)出信號(hào)。pthread_cancel。同上。這是對(duì)線程的殘酷殺戮。那,線程可能正在做其他事情。從主線程關(guān)閉監(jiān)聽套接字,以使accept()中止。這不能可靠地工作。一些約束:如果解決方案涉及使偵聽套接字不阻塞,那很好。但是我不想接受這樣的解決方案,即每隔幾秒鐘通過一次select調(diào)用喚醒線程以檢查退出條件。退出的線程條件可能與進(jìn)程退出無關(guān)。本質(zhì)上,我要尋找的邏輯看起來像這樣。void* WorkerThread(void* args){    DoSomeImportantInitialization();  // initialize listen socket and some thread specific stuff    while (HasExitConditionBeenSet()==false)    {        listensize = sizeof(listenaddr);        int sock = accept(listensocket, &listenaddr, &listensize);        // check if exit condition has been set using thread safe semantics        if (HasExitConditionBeenSet())        {            break;        }        if (sock < 0)        {            printf("accept returned %d (errno==%d)\n", sock, errno);        }        else        {            HandleNewNetworkCondition(sock, &listenaddr);        }    }    DoSomeImportantCleanup(); // close listen socket, close connections, cleanup etc..    return NULL;}void SignalHandler(int sig){    printf("Caught CTRL-C\n");}void NotifyWorkerThreadToExit(pthread_t thread_handle){    // signal thread to exit}int main(){    void* ptr_ret= NULL;    pthread_t workerthread_handle = 0;    pthread_create(&workerthread, NULL, WorkerThread, NULL);    signal(SIGINT, SignalHandler);    sleep((unsigned int)-1); // sleep until the user hits ctrl-c    printf("Returned from sleep call...\n");    SetThreadExitCondition(); // sets global variable with barrier that worker thread checks on    // this is the function I'm stalled on writing    NotifyWorkerThreadToExit(workerthread_handle);    // wait for thread to exit cleanly    pthread_join(workerthread_handle, &ptr_ret);    DoProcessCleanupStuff();}
查看完整描述

3 回答

?
HUH函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以使用管道來通知線程您希望其退出。然后,您可以select()在管道和監(jiān)聽套接字上進(jìn)行選擇的呼叫。


例如(編譯但未完全測(cè)試):


// NotifyPipe.h

#ifndef NOTIFYPIPE_H_INCLUDED

#define NOTIFYPIPE_H_INCLUDED


class NotifyPipe

{

        int m_receiveFd;

        int m_sendFd;


    public:

        NotifyPipe();

        virtual ~NotifyPipe();


        int receiverFd();

        void notify();

};


#endif // NOTIFYPIPE_H_INCLUDED


// NotifyPipe.cpp


#include "NotifyPipe.h"


#include <unistd.h>

#include <assert.h>

#include <fcntl.h>


NotifyPipe::NotifyPipe()

{

    int pipefd[2];

    int ret = pipe(pipefd);

    assert(ret == 0); // For real usage put proper check here

    m_receiveFd = pipefd[0];

    m_sendFd = pipefd[1];

    fcntl(m_sendFd,F_SETFL,O_NONBLOCK);

}



NotifyPipe::~NotifyPipe()

{

    close(m_sendFd);

    close(m_receiveFd);

}



int NotifyPipe::receiverFd()

{

    return m_receiveFd;

}



void NotifyPipe::notify()

{

    write(m_sendFd,"1",1);

}

然后select使用receiverFd(),并使用通知終止notify()。


查看完整回答
反對(duì) 回復(fù) 2019-12-03
?
三國紛爭(zhēng)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

使用shutdown()調(diào)用關(guān)閉套接字。這將喚醒所有阻塞的線程,同時(shí)保持文件描述符有效。

close()在描述符上,另一個(gè)線程B使用具有內(nèi)在的危險(xiǎn):另一個(gè)線程C可能會(huì)打開一個(gè)新的文件描述符,然后線程B將使用該文件描述符而不是已關(guān)閉的文件描述符。dup2()/dev/null到就避免了這個(gè)問題,但沒有醒來阻塞的線程可靠。

請(qǐng)注意,shutdown()僅適用于套接字-對(duì)于其他類型的描述符,您可能需要select + pipe-to-self或cancel方法。


查看完整回答
反對(duì) 回復(fù) 2019-12-03
?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果pthread實(shí)現(xiàn)無法正確實(shí)現(xiàn)取消,則pthread_cancel取消被accept()阻塞的線程是有風(fēng)險(xiǎn)的,也就是說,如果線程創(chuàng)建了一個(gè)套接字,則在返回代碼之前,將為其調(diào)用pthread_cancel(),該線程為取消,并且新創(chuàng)建的套接字泄漏。盡管FreeBSD 9.0和更高版本沒有這種競(jìng)爭(zhēng)狀況問題,但是您應(yīng)該首先檢查操作系統(tǒng)。


查看完整回答
反對(duì) 回復(fù) 2019-12-03
  • 3 回答
  • 0 關(guān)注
  • 714 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)