首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

难过啊该如何处理

2012-02-06 
难过啊~创建两个生成者进程和消费者进程,用P/V操作实现他们之间的同步。(用线程实现)#includestdio.hmain

难过啊~
创建两个生成者进程和消费者进程,用P/V操作实现他们之间的同步。(用线程实现)
 
#include<stdio.h>
main(void)
{
 deposit(data):
 begin
  p(avail)
  p(mutex)
  v(full)
  v(mutex)
 end
 remove(data)
  begin
  p(full)
  p(mutex)
  v(avail)
  v(mutex)
 End
}
 
帮我看下问题大不…在哪谢谢!!还有多个进程跟2个进程应该一样吧! 最后还有怎么用线程实现? 3Q!!

[解决办法]
我怎么看不懂啊?这是C语言吗?
1、不是以分号结尾
2、deposit(data)?main函数内定义函数?
3、begin? end?

[解决办法]
孙鑫老师VC++里的一段使用互斥对象的讲解代码,可能对你有帮助哈。
#include <windows.h>
#include <iostream.h>

DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);

DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
);
int index=0;
int tickets=100;
HANDLE hMutex;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
/*while(index++<1000)
cout<<"main thread is running"<<endl;*/
//hMutex=CreateMutex(NULL,TRUE,NULL);
hMutex=CreateMutex(NULL,TRUE,"tickets");
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
Sleep(4000);
//Sleep(10);
}

DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
/*while(index++<1000)
cout<<"thread1 is running"<<endl;*/

/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/

WaitForSingleObject(hMutex,INFINITE);
cout<<"thread1 is running"<<endl;
return 0;
}

DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
)
{

/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread2 is running"<<endl;
return 0;
}

热点排行