无法进行同步,每次运行要么就是只有线程1运行,要么就是只有线程2运行,不知道代码有没有问题,检查很多次了,应该没有写错,难道是系统的问题吗?win7,vs2008下调试的,望高人指点下,,代码如下...#include <Windows.h>
#include <iostream>
using namespace std;DWORD WINAPI Fun1Proc(
  LPVOID lpParameter
  );DWORD WINAPI Fun2Proc(
  LPVOID lpParameter
  );int tickets = 100;CRITICAL_SECTION g_cs;
void main()
{
//创建临界区资源
InitializeCriticalSection(&g_cs);
HANDLE hThread1,hThread2;
//创建线程之后,通过CREATE_SUSPENDED先挂起
hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);
hThread2 = CreateThread(NULL, 0, Fun2Proc, NULL, 0, NULL);

CloseHandle(hThread1);
CloseHandle(hThread2); Sleep(4000);
//删除临界区资源
DeleteCriticalSection(&g_cs);
}DWORD WINAPI Fun1Proc(
  LPVOID lpParameter
  )
{
while (TRUE)
{
EnterCriticalSection(&g_cs);
if (tickets > 0)
{
Sleep(1);
cout << "thread1 sell ticket : " << tickets-- << endl;
}
else
break;
LeaveCriticalSection(&g_cs);
}
return 0;
}DWORD WINAPI Fun2Proc(
  LPVOID lpParameter
  )
{
while (TRUE)
{
EnterCriticalSection(&g_cs);
if (tickets > 0)
{
Sleep(1);
cout << "thread2 sell ticket : " << tickets-- << endl;
}
else
break;
LeaveCriticalSection(&g_cs);
}
return 0;
}

解决方案 »

  1.   

    你的main sleep(4000)就直接退出了,搞个循环啥的等着试试
      

  2.   

    测试结果: 有问题么?thread1 sell ticket : 100
    thread2 sell ticket : 99
    thread1 sell ticket : 98
    thread2 sell ticket : 97
    thread1 sell ticket : 96
    thread2 sell ticket : 95
    thread1 sell ticket : 94
    thread2 sell ticket : 93
    thread1 sell ticket : 92
    thread2 sell ticket : 91
    thread1 sell ticket : 90
    thread2 sell ticket : 89
    thread1 sell ticket : 88
    thread2 sell ticket : 87
    thread1 sell ticket : 86
    thread2 sell ticket : 85
    thread1 sell ticket : 84
    thread2 sell ticket : 83
    thread1 sell ticket : 82
    thread2 sell ticket : 81
    thread1 sell ticket : 80
    thread2 sell ticket : 79
    thread1 sell ticket : 78
    thread2 sell ticket : 77
    thread1 sell ticket : 76
    thread2 sell ticket : 75
    thread1 sell ticket : 74
    thread2 sell ticket : 73
    thread1 sell ticket : 72
    thread2 sell ticket : 71
    thread1 sell ticket : 70
    thread2 sell ticket : 69
    thread1 sell ticket : 68
    thread2 sell ticket : 67
    thread1 sell ticket : 66
    thread2 sell ticket : 65
    thread1 sell ticket : 64
    thread2 sell ticket : 63
    thread1 sell ticket : 62
    thread2 sell ticket : 61
    thread1 sell ticket : 60
    thread2 sell ticket : 59
    thread1 sell ticket : 58
    thread2 sell ticket : 57
    thread1 sell ticket : 56
    thread2 sell ticket : 55
    thread1 sell ticket : 54
    thread2 sell ticket : 53
    thread1 sell ticket : 52
    thread2 sell ticket : 51
    thread1 sell ticket : 50
    thread2 sell ticket : 49
    thread1 sell ticket : 48
    thread2 sell ticket : 47
    thread1 sell ticket : 46
    thread2 sell ticket : 45
    thread1 sell ticket : 44
    thread2 sell ticket : 43
    thread1 sell ticket : 42
    thread2 sell ticket : 41
    thread1 sell ticket : 40
    thread2 sell ticket : 39
    thread1 sell ticket : 38
    thread2 sell ticket : 37
    thread1 sell ticket : 36
    thread2 sell ticket : 35
    thread1 sell ticket : 34
    thread2 sell ticket : 33
    thread1 sell ticket : 32
    thread2 sell ticket : 31
    thread1 sell ticket : 30
    thread2 sell ticket : 29
    thread1 sell ticket : 28
    thread2 sell ticket : 27
    thread1 sell ticket : 26
    thread2 sell ticket : 25
    thread1 sell ticket : 24
    thread2 sell ticket : 23
    thread1 sell ticket : 22
    thread2 sell ticket : 21
    thread1 sell ticket : 20
    thread2 sell ticket : 19
    thread1 sell ticket : 18
    thread2 sell ticket : 17
    thread1 sell ticket : 16
    thread2 sell ticket : 15
    thread1 sell ticket : 14
    thread2 sell ticket : 13
    thread1 sell ticket : 12
    thread2 sell ticket : 11
    thread1 sell ticket : 10
    thread2 sell ticket : 9
    thread1 sell ticket : 8
    thread2 sell ticket : 7
    thread1 sell ticket : 6
    thread2 sell ticket : 5
    thread1 sell ticket : 4
    thread2 sell ticket : 3
    thread1 sell ticket : 2
    thread2 sell ticket : 1
      

  3.   

    Sleep(4000); 改成 getchar();你在线程里面设置断点调试的同时 主线程已经退出了.
      

  4.   

    while (TRUE)
        {
            EnterCriticalSection(&g_cs);
            if (tickets > 0)
            {
                Sleep(1);
                cout << "thread2 sell ticket : " << tickets-- << endl;
            }
            else
                break;
            LeaveCriticalSection(&g_cs);
        }
    --------------------------
    你的while循环中的else语句下面就直接break了,并没有调用LeaveCriticalSection()来释放临界区
      

  5.   


    #include <Windows.h>
    #include <iostream>
    using namespace std;DWORD WINAPI Fun1Proc(
                          LPVOID lpParameter
                          );DWORD WINAPI Fun2Proc(
                          LPVOID lpParameter
                          );int tickets = 100;CRITICAL_SECTION g_cs;
    void main()
    {
        //´´½¨ÁÙ½çÇø×ÊÔ´
        InitializeCriticalSection(&g_cs);
        //HANDLE hThread1,hThread2;
    HANDLE hThread[2] = {0};
        //´´½¨Ïß³ÌÖ®ºó,ͨ¹ýCREATE_SUSPENDEDÏȹÒÆð
        hThread[0] = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);
        hThread[1] = CreateThread(NULL, 0, Fun2Proc, NULL, 0, NULL);
         WaitForMultipleObjects(sizeof(hThread)/sizeof(hThread[0]), hThread, TRUE, INFINITE);    CloseHandle(hThread[0]);
        CloseHandle(hThread[1]);

       // Sleep(4000);
        //ɾ³ýÁÙ½çÇø×ÊÔ´
        DeleteCriticalSection(&g_cs);
    }DWORD WINAPI Fun1Proc(
                          LPVOID lpParameter
                          )
    {
        while (TRUE)
        {
           EnterCriticalSection(&g_cs);
            if (tickets > 0)
            {
                Sleep(1);
                cout << "thread1 sell ticket : " << tickets-- << endl;
            }
            else
    {
    LeaveCriticalSection(&g_cs);
                break;
    }
            LeaveCriticalSection(&g_cs);
        }
        return 0;
    }DWORD WINAPI Fun2Proc(
                          LPVOID lpParameter
                          )
    {
        while (TRUE)
        {
            EnterCriticalSection(&g_cs);
            if (tickets > 0)
            {
                Sleep(1);
                cout << "thread2 sell ticket : " << tickets-- << endl;
            }
            else
    {
    LeaveCriticalSection(&g_cs);
                break;
    }
    LeaveCriticalSection(&g_cs);
        }
        return 0;
    }
      

  6.   

    else是tickets小于或者等于0的时候才执行的啊
      

  7.   

    系统问题吧。用了LZ和5楼的代码:在XP下,效果如2楼一样,线程争夺战。而在win7下,却仿佛只有一条线程运行似的(很偶尔才看到另一条线程出现)。
      

  8.   

    是啊,就是这么说就是win7的问题了,郁闷啊~
      

  9.   

    WindowsXP中多线程切换调度时间为20ms ,所以低于20ms的精度用多线程很难做
    Win7不清楚
    你上面代码写的有问题- -) 线程切换需要条件的 你给的条件太恶劣了。你的Sleep应该在临界区 外面 放在里面两边都卡住了。
      

  10.   

    xp的临界区是FIFO的,win2003和以上的操作系统可能不是的。