O_NONBLOCKモードで名前付きパイプを作成し、別のスレッドで「SELECT」メソッドを使用して読み取りイベントをリッスンしようとしています。メインスレッドでスリープ時間を設定した後、プログラムを閉じようとすると問題が発生します。名前付きパイプのファイル記述子がcloseメソッドを使用して閉じられると、select操作はすぐに停止し、何らかの値を返すはずです。しかし、残念ながら、ファイル記述子が閉じられ、selectメソッドを実行するスレッドがハングした場合、select操作は反応しません...
それを解決する方法はありますか?サンプルコードは以下のとおりです。
#include <pthread.h>
#include <limits.h>
#include <cctype>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <exception>
#define BUFFER PIPE_BUF
#define LPVOID void *
#define BOOL int
#define TRUE 1
#define CONST const
#define CHAR char
class CPipeTest
{
    public:
    int fd;
    int nfd;
    fd_set rfd;
    pthread_t t;
    CPipeTest() {};
    ~CPipeTest() {};
    static LPVOID fnExecuteThread(LPVOID lpParam)
    {
        ((CPipeTest*)lpParam)->fnRunThread();
        return NULL;
    }
    BOOL fnRunThread()
    {
        printf("Going to listen...\r\n");
        select(nfd, &rfd, NULL, NULL, NULL);
        printf("Close listener...\r\n");
        return TRUE;
    }
    void fnInit()
    {
        CONST CHAR * name = "./test_fifo1";
        mkfifo(name, 0777);
        fd = open(name, O_NONBLOCK | O_RDONLY);
        nfd = fd + 1;
        FD_ZERO(&rfd);
        FD_SET(fd, &rfd);
        pthread_create( &t, NULL, fnExecuteThread, (LPVOID)this);
        sleep(30);
        printf("Close file descriptor - listener should be closed automatically and immediately\r\n");
        close(fd);
        printf("Descriptor closed wait for thread to to be closed\r\n");
        pthread_join(t, NULL);
        printf("Thread is closed - everything is fine\r\n");
    }
};
int main()
{
    CPipeTest pi;
    pi.fnInit();
    return 0;
}