| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 340 人关注过本帖
标题:百思不得其解。。。求鞭挞,求解救
只看楼主 加入收藏
yd3394909
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-11-6
收藏
 问题点数:0 回复次数:1 
百思不得其解。。。求鞭挞,求解救

我头疼啊。。。为什么不能够实现互斥。。。。菜菜求大神指点

#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
bool g_continue=true;
int n,m,t=0;
int k=1;
int a[10];
int b[10];
DWORD  WINAPI Thread1(LPVOID lpPara); //线程1
DWORD  WINAPI Thread2(LPVOID lpPara); //线程1
HANDLE huchi;
int main(){

    HANDLE hThread1, hThread2;//两个线程的handle
    DWORD ThreadId1, ThreadId2; //两个线程的线程ID
    huchi=CreateMutex(NULL,FALSE,NULL);
    hThread1 = CreateThread(NULL,0,Thread1,NULL,0,&ThreadId1);  //创建线程1
    if(hThread1 == NULL)
        return -1;

    hThread2 = CreateThread(NULL,0,Thread2,NULL,0,&ThreadId2); //创建线程2
    if(hThread2 == NULL)
        return -1;

    if(getchar()){ //按回车后终止程序运行
        g_continue = false;
    }

    //等待子进程结束
    WaitForSingleObject(hThread1,INFINITE);
    WaitForSingleObject(hThread2,INFINITE);

    //释放子进程资源
    CloseHandle(hThread1);
    CloseHandle(hThread2);
    system("pause");
}

//线程1执行的代码
DWORD  WINAPI Thread1(LPVOID lpPara)
{  
    WaitForSingleObject(huchi,INFINITE);
    while(g_continue)
 {
  for(n=0;n<10;n++)
    {if (a[n]==0)
     {a[n]=1;
      b[n]=k;
      k=k+1;
      break;}}
  cout<<"生产者将生产编号"<<k-1<<"放入缓冲单元"<<n<<endl;
  cout<<"缓冲单元编号"<<"       产品编号        "<<"缓冲单元状态"<<endl;
  for(n=0;n<10;n++)
  {cout<<"    "<<n<<"                    "<<b[n]<<"               "<<a[n]<<endl;}

  ReleaseMutex (huchi);
 }
     return 0;
}


//线程2执行的代码
DWORD  WINAPI Thread2(LPVOID lpPara)
{      
    WaitForSingleObject(huchi,INFINITE);
 while(g_continue)
 {
  for(n=0;n<10;n++)
    {if (a[m]==1)
     {a[m]=0;
      t=b[m];
      b[m]=0;
      break;}}
  cout<<"消费者从缓冲单元"<<m<<"将产品"<<t<<"取出"<<endl;
  cout<<"缓冲单元编号"<<"       产品编号        "<<"缓冲单元状态"<<endl;
  for(m=0;m<10;m++)
   {cout<<"   "<<m<<"                     "<<b[m]<<"               "<<a[m]<<endl;}

  ReleaseMutex (huchi);
 }
     return 0;
}
搜索更多相关主题的帖子: 能够 include 
2012-11-06 09:43
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
看不懂,瞎改的
程序代码:
#include <windows.h>
#include <iostream>
using namespace std;

HANDLE hMutex;
DWORD WINAPI Thread1(LPVOID lpPara); //线程1
DWORD WINAPI Thread2(LPVOID lpPara); //线程1

volatile bool g_continue = true;
volatile int a[10] = { 0 };
volatile int b[10];

int main()
{
    hMutex = CreateMutex(NULL,FALSE,NULL);
    if( !hMutex )
        return 1;

    DWORD ThreadId1;
    HANDLE hThread1 = CreateThread(NULL,0,&Thread1,NULL,0,&ThreadId1);
    if( !hThread1 )
    {
        CloseHandle( hMutex );
        return 2;
    }
    DWORD ThreadId2;
    HANDLE hThread2 = CreateThread(NULL,0,&Thread2,NULL,0,&ThreadId2);
    if( !hThread2 )
    {
        CloseHandle( hThread1 );
        CloseHandle( hMutex );
        return 3;
    }

    if( getchar() ) { //按回车后终止程序运行
        g_continue = false;
    }

    //等待子进程结束
    WaitForSingleObject(hThread1,INFINITE);
    WaitForSingleObject(hThread2,INFINITE);

    //释放子进程资源
    CloseHandle(hThread2);
    CloseHandle(hThread1);
    CloseHandle( hMutex );
    return 0;
}

DWORD WINAPI Thread1(LPVOID lpPara)
{
    int id = 0;

    while(g_continue)
    {
        WaitForSingleObject(hMutex,INFINITE);
        size_t i = 0;
        for( ; i<_countof(a); ++i )
        {
            if( a[i] == 0 )
            {
                a[i] = 1;
                b[i] = id++;
                break;
            }
        }
        if( i < _countof(a) )
        {
            cout << i << ": +" << id-1 << endl;
        }
        ReleaseMutex (hMutex);

        if( id == 20 )
            break;
    }
    return 0;
}

DWORD WINAPI Thread2( LPVOID lpPara )
{     

    while(g_continue)
    {
        WaitForSingleObject(hMutex,INFINITE);
        size_t i = 0;
        for( ; i<_countof(a); ++i )
        {
            if( a[i] == 1 )
            {
                a[i] = 0;
                break;
            }
        }
        if( i < _countof(a) )
        {
            int id = b[i];
            cout << i << ": -" << id << endl;
        }
        ReleaseMutex (hMutex);
    }
    return 0;

2012-11-06 13:19
快速回复:百思不得其解。。。求鞭挞,求解救
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016429 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved