简体   繁体   English

具有互斥和semapohres的多线程程序

[英]multithread program with mutexes and semapohres

There are 3 series' to compute using multithread. 使用多线程可以计算3个系列。
To access a series you need to wait for semaphore, and after that you need to know if you are builder thread or cleaning thread. 要访问一个系列,您需要等待信号量,然后您需要知道您是构建器线程还是清洗线程。
Everything works great when I have num of threads which all of them has what to do and there are no 'floating' thread that cannot access semaphore or mutex. 当我有许多线程都需要执行的操作并且没有无法访问信号量或互斥量的“浮动”线程时,一切工作都很好。
If I do have too many threads and the dont have what to do the program get stuck unless after WaitSemRes/WaitMutexRes I kill that thread but I dont want to do that bc I loosing to much computing time. 如果我确实有太多的线程,而没有做什么该程序,则卡住,除非在WaitSemRes / WaitMutexRes之后我杀死了该线程,但我不想这样做,因为我浪费了很多计算时间。 I just want them to keep waiting until they have something to do. 我只希望他们继续等待,直到他们有事情要做。
Am I missing something? 我想念什么吗?

/* Thread is trying to enter specific series, if couldnt - try the next one*/

WaitSemRes = WaitForSingleObject(main_series[series_idx].semaphore, 0L);
    if (WaitSemRes == WAIT_OBJECT_0) {

        /* Check if i'm the cleaning thread */
        if (main_series[series_idx].ready_to_clean) {

            /* Catching series mutex to clean it alone */
            WaitMutexRes = WaitForSingleObject(main_series[series_idx].mutex, INFINITE);
            if (WaitMutexRes == WAIT_OBJECT_0) 
                // do something //
            }
            else {
                ExitThread(0);
            }

             // do stuff
    else {
        ExitThread(0);
    }

You could use a shutdown event to signalize the threads when there's no more work to be done, this way they can keep waiting indefinitely. 您可以使用shutdown事件在没有更多工作要做时发出信号,以这种方式它们可以无限期地等待。 It can be created using CreateEvent , then all you need to do is to make your worker threads wait upon it. 可以使用CreateEvent创建它,然后您要做的就是让工作线程等待它。 When you all work is done you can make all threads exit almost simultaneously by using SetEvent, and then wait upon a HANDLE array that contains all the thread handles 完成所有工作后,可以使用SetEvent使所有线程几乎同时退出,然后等待包含所有线程句柄的HANDLE数组

For details regarding how to wait on multiple objects you can refer to WaitForMultipleObjects Example 有关如何等待多个对象的详细信息,请参阅WaitForMultipleObjects示例。

Worker thread 工作线程

int thread_function(...) {
[...]
    while(WaitForSingleObject(thread_shutdown_event, 0) != WAIT_OBJECT_0) {
        // Keep trying to process data until the event is signalized
    }
    // Exit thread
    return 0;
}

Pool handling functions 池处理功能

void end_thread_pool(...) {
    HANDLE wait_result;

    if(thread_shutdown_event == NULL)
        return;
    // Activate shutdown event
    SetEvent(thread_shutdown_event);
    // Wait upon thread handle list until all threads can return safely
    wait_result = (HANDLE)WaitForMultipleObjects(...);
    if(wait_result != WAIT_OBJECT_0) {
        // Failed to wait
    }
    CloseHandle(thread_shutdown_event);
[...]
    return;
}


void init_thread_pool(...) {
[...]
    thread_shutdown_event = CreateEvent(NULL, // Default security attributes
                                        TRUE, // Manual reset?
                                        FALSE,// Starting value
                                        TEXT("Thread Shutdown event"));// Event name
    if(thread_shutdown_event == NULL) {
        // Failed to create event
        return;
    }
    // Initiates threads and also puts their HANDLEs in a array
    // that is going to be used in end_thread_pool to wait
    // See 'WaitForMultipleObjects Example'
    thread_create(thread_function, number_of_threads);
[...]
    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM