c++ - 我想让线程的执行顺序依次执行

标签 c++ c multithreading winapi

#include <windows.h>
#include <iostream>
using namespace std;

int count = 0;

DWORD WINAPI Tf1 ( LPVOID n )
{
    HANDLE hEvent = OpenEvent ( EVENT_ALL_ACCESS , false, (LPCWSTR)"MyEvent" );
    if ( !hEvent ) { return -1; }
    // Loop through and wait for an event to occur
    for ( ;; )
    {
        // Wait for the Event
        WaitForSingleObject ( hEvent, INFINITE );
        count++;
        printf("In function1, Counter value: %d\n",count);
        //    No need to Reset the event as its become non signaled as soon as
        //    some thread catches the event.
        if( count > 7 ) return 0;
    }
    CloseHandle(hEvent);
    return 0;
}

DWORD WINAPI Tf2 ( LPVOID n )
{

    HANDLE hEvent = OpenEvent ( EVENT_MODIFY_STATE , false, (LPCWSTR)"MyEvent" );
    if ( !hEvent ) return -1; 

    for ( ;; )
    {
        if( count % 2 == 0) 
            SetEvent (hEvent);
        else
        {
            count++;
            printf("In function2, Counter value: %d\n",count);
        }
        //    No need to Reset the event as its become non signaled as soon as
        //    some thread catches the event.
        if( count > 7 ) return 0;
    }
    CloseHandle(hEvent);
    return 0;
}

int main()
{
    //    Create an Auto Reset Event which automatically reset to 
    //    Non Signalled state after being signalled
    HANDLE     hEvent = CreateEvent ( NULL , false , false , (LPCWSTR)"MyEvent" );
    if ( !hEvent ) return -1;
    //    Create a Thread Which will wait for the events to occur
    HANDLE mutex = CreateMutex(NULL, FALSE, NULL);

    DWORD Id;
    HANDLE hThrd1 = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE)Tf1,0,0,&Id );
    HANDLE hThrd2 = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE)Tf2,0,0,&Id );

    if ( !hThrd1 ) { CloseHandle (hEvent); return -1; }
    if ( !hThrd2 ) { CloseHandle (hEvent); return -1; }
    // Wait for a while before continuing....
    Sleep ( 1000 );
    // Give the signal twice as the thread is waiting for 2 signals to occur

    // Wait for the Thread to Die
    WaitForSingleObject ( hThrd1, INFINITE );
    WaitForSingleObject ( hThrd2, INFINITE );

    CloseHandle ( hThrd1 );
    CloseHandle ( hThrd2 );
    CloseHandle ( hEvent );

    system ( "PAUSE" );
    return 0;
}

O/P:

In function1, Counter value: 1
In function1, Counter value: 3
In function2, Counter value: 2
In function2, Counter value: 4
In function2, Counter value: 6
In function1, Counter value: 5
In function1, Counter value: 7
In function2, Counter value: 8

所需的 O/P:

In function1, Counter value: 1
In function2, Counter value: 2
In function1, Counter value: 3
In function2, Counter value: 4
In function1, Counter value: 5
In function2, Counter value: 6
In function1, Counter value: 7
In function2, Counter value: 8

我知道我没有利用线程的力量。但我的要求是这样的。我怎样才能实现这个目标?

最佳答案

这段代码中没有任何东西可以阻止竞争条件。您要么需要两个事件,以便线程与信号和等待打乒乓球,要么用关键部分保护计数器。

选项 1:

// Thread 1
{
    increment counter
    Signal event 2
    Wait on event 1
}

// Thread 2
{
    Wait on event 2
    increment counter
    Signal event 1
}

选项 2:(使用您现有的范例)

// Thread 1
{
    Wait on event
    Acquire mutex
    if count is even
        increment counter
    end
    Release mutex
}

// Thread 2
{
    Acquire mutex
    if count is even
        Signal event
    else
        increment counter
    end
    Release mutex
}

请注意,选项二在线程 2 中有点困惑...当线程 1 响应事件时,它可能会旋转多次。在这方面,您也可以完全删除该事件并让两个线程都退出...如果您想避免这种情况,请使用我的选项 1。

还有其他方法可以采用,包括使用互锁增量,但无论如何请尝试上述方法之一。

关于c++ - 我想让线程的执行顺序依次执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13147243/

相关文章:

c - OpenMP 范围界定 - 段。共享错误()

c# - 以编程方式双击系统托盘图标 Windows xp/7

c++ - 为什么这个输出看起来不确定? (是 sprintf、printf 还是十六进制文字的语法?)

C 程序将值读入两个单独的部分?

c - 为什么数组可以存储比应有的更多的数据?

c++ - Boost,指向互斥量的指针,这行得通吗? boost::mutex 和 std::vector,不可复制的问题

c++ - C++/gSoap 2.8.2 中奇怪的结构成员对齐

c++ - 如何将命令行参数传递给 c 程序

c - C 中的循环在 Repl.it 中退出,但在使用 gcc 在本地运行时不退出

java同步访问不同的临界区