刚学线程,高手指点下semaphores的用法 刚学semaphores,我就是不明白怎么能让多个线程同时进入资源访问?书本上也没有例子,那位大哥能指点一下啊?不胜感激!最好附带一下应用的例子 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.vckbase.com/document/viewdoc/?id=1708 所谓的同时,其实是并发执行的,CPU给每个线程都分配了一定的时间,轮转调度嘛 我看了heksn 的关于semaphores的例子,可我以为这样会造成数据错误,那到底semaphores到底何种情况下才适用? 比如说一个网站的页面,我们可以设定最大访问量,这个时候我们就可以用到semaphores 那么内存数据可不可以使用semaphores呢?我刚才看了一下,如果对某一数组sem[100]采用semaphores,发生了数据错误,怎样使用semaphores才能保证分别输出sem[100]不发生错误呢? semaphores只能用在网站上么?谁能举个内存数组使用semaphores的例子啊? DWORD WINAPI Func1(LPVOID lpParameter);int index = 0;int a[10]={1,2,3,4,5,6,7,8,9,10}CRITICAL_SECTION cSection;int main(){ HANDLE hThread1; hThread1 = CreateThread(NULL,0,Func1,NULL,0,NULL); CloseHandle(hThread1); InitializeCriticalSection(&cSection); while(index < 10) { EnterCriticalSection(&cSection); index++; cout <<"main thread is running - " << index <<endl; LeaveCriticalSection(&cSection); // Breakpoint added at this line } DeleteCriticalSection(&cSection); return 0;}DWORD WINAPI Func1(LPVOID lpParameter){ while(index < 10) { EnterCriticalSection(&cSection); index++; cout<<a[index]<<endl; cout <<"thread1 is running - " << index <<endl; LeaveCriticalSection(&cSection); } return 0;} semaphores 是 信号量内核对象 主要 是 在 线程 同步 的 时候 使 多个线程 可以 同时 对 数据 进行 访问。 怎样打开和关闭指定程序? 线程句柄会不会因为线程的挂起而变为有信号 对话框显示问题 我用VC向导生成了一个基于formview的MFC程序,请问怎样通过菜单动态创建forview窗口?? 请问如何在按钮控件中使用一幅位图? CButton的WM_LBUTTONDOWN消息捕获 向高手求助:如何在Picture控件中使用滚动条? 小弟想编程实现局琙网内邮件发送功能,请问各位大哥需要哪些预备知识? 请问不用设备驱动应用程序怎么控制usb传输? 创建记录的问题 如何将一个CString型变量赋值给Float类型 ST问题简单问题
DWORD WINAPI Func1(LPVOID lpParameter);
int index = 0;
int a[10]={1,2,3,4,5,6,7,8,9,10}
CRITICAL_SECTION cSection;int main()
{
HANDLE hThread1;
hThread1 = CreateThread(NULL,0,Func1,NULL,0,NULL);
CloseHandle(hThread1); InitializeCriticalSection(&cSection);
while(index < 10)
{
EnterCriticalSection(&cSection);
index++;
cout <<"main thread is running - " << index <<endl;
LeaveCriticalSection(&cSection); // Breakpoint added at this line
}
DeleteCriticalSection(&cSection);
return 0;
}DWORD WINAPI Func1(LPVOID lpParameter)
{
while(index < 10)
{
EnterCriticalSection(&cSection);
index++;
cout<<a[index]<<endl;
cout <<"thread1 is running - " << index <<endl;
LeaveCriticalSection(&cSection);
}
return 0;
}