C++多线程怎么实现

anny_1103 2008-07-28 01:49:14
如题!最好有例子
...全文
120405 45 打赏 收藏 转发到动态 举报
写回复
用AI写文章
45 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaiyuan_ky 2012-09-23
  • 打赏
  • 举报
回复
mark
zhaotai7661 2012-08-23
  • 打赏
  • 举报
回复
说的还是比较细的,不过还是不大懂,照着练练先
goyoungco 2012-08-11
  • 打赏
  • 举报
回复
好东西,来看看
chpLOVElgz 2012-07-27
  • 打赏
  • 举报
回复
学习了。
lcya86 2012-07-18
  • 打赏
  • 举报
回复
学习了
changecode 2012-06-02
  • 打赏
  • 举报
回复
麻烦大家帖代码规范点,不然有复制的嫌疑,看起来非常头大。
江流儿z 2012-05-28
  • 打赏
  • 举报
回复
mark
jquery83 2012-05-14
  • 打赏
  • 举报
回复
多点注释就更好了
lq402500328 2012-04-27
  • 打赏
  • 举报
回复
那两个例子让这个小白终于有点儿摸着头脑了~!
不过要是注释再多些就好了,那些参数NULL啊啥的是带的什么,解释下就好了...
zha_zi 2012-01-12
  • 打赏
  • 举报
回复
小手一抖10分到手
guanzhongw 2011-12-12
  • 打赏
  • 举报
回复
int _tmain(int argc, _TCHAR* argv[])
{
Share<int> s ;

product<int> p ;

p.SetShare( s ) ;

customer<int> c ;

c.SetShare(s) ;

c.StartCustomer() ;

p.StartProduct() ;

getchar() ;

return 0;
}
guanzhongw 2011-12-12
  • 打赏
  • 举报
回复
#pragma once

#include <Windows.h>

#include <iostream>
#include <vector>
#include <map>
#include <iterator>
#include <string>
#include <queue>

using namespace std ;

DWORD WINAPI ProductThreadFunc(PVOID pvParam) ;
DWORD WINAPI CustomerThreadFunc(PVOID pvParam);

template<class T> class Share
{
private:

vector<T> store ; // 存放容器

int maxValue ; // 容器最大值

HANDLE hMutex ; // 声明一个互斥对象

queue<T> Q; // 可以使用队列,队列是先进先出的 ( front() , pop() )

public:

Share()
{
hMutex = CreateMutex(NULL, false, L"Share");
}

~Share()
{

}

void SetMax(int max)
{
maxValue = max ;
}

bool PutOneObject(T object)
{
bool bSuccess = false ;

try
{
WaitForSingleObject(hMutex, INFINITE);

store.push_back( object ) ;

printf("input object %d \r\n",object) ;

bSuccess = true ;
}
catch(...)
{

}

ReleaseMutex(hMutex);

return bSuccess ;
}

T GetOneObject()
{
T object = NULL ;

try
{
WaitForSingleObject(hMutex, INFINITE); //申请资源后,上面的线程就申请不到了,就得等待,知道主线程释放资源

if( store.size() > 0 )
{
object = store.front() ;

printf("Get Object:%d\r\n",object) ;

store.erase(store.begin());

}

}
catch(...)
{

}

ReleaseMutex(hMutex); //释放资源后,上面的线程可以申请资源了

return object ;

}

int StoreSize()
{
int iCount = 0 ;

try
{
WaitForSingleObject(hMutex, INFINITE); //申请资源后,上面的线程就申请不到了,就得等待,知道主线程释放资源

iCount = store.size() ;

}
catch(...)
{

}

ReleaseMutex(hMutex); //释放资源后,上面的线程可以申请资源了

return iCount ;
}


private:


};


template<class T> class product
{
public:
product()
{
}
~product()
{
}

void SetShare( Share<T> &obj )
{
p = &obj ;
}


void StartProduct()
{
HANDLE hThread ;

//创建线程
hThread=CreateThread(NULL,0,ProductThreadFunc,this,0,NULL);

}

bool ProductObject()
{
bool bSuccess =false ;

try
{
int iMax = 1000 ;

p->SetMax(iMax) ;

for( int i = 0 ;i < iMax ; i ++ )
{
T obj = i ;

p->PutOneObject( obj ) ;

::Sleep(1) ;
}

}
catch(...)
{

}

return bSuccess ;
}

private:
Share<T> *p;
};


template<class T> class customer
{
public:
customer()
{
}
~customer()
{
}

void SetShare( Share<T> &obj )
{
c = &obj ;
}


void StartCustomer()
{
HANDLE hThread ;

//创建线程
hThread=CreateThread(NULL,0,CustomerThreadFunc,this,0,NULL);

}

bool CustomerObject()
{
bool bSuccess = false ;

try
{
while(true)
{
if( c->StoreSize() > 0 )
{
T o = c->GetOneObject() ;

// printf("Get the Object:%d\r\n",o) ;
}
::Sleep(1) ;
}
}
catch( ... )
{

}
return bSuccess ;
}

private:
Share<T> *c;

};



在 .cpp 中 写如下:

// 生产者线程
DWORD WINAPI ProductThreadFunc(PVOID pvParam)
{
product<int> *p = (product<int> *)pvParam ;

DWORD dwResult = 0;

p->ProductObject() ;

return(dwResult);
}


// 消费者线程
DWORD WINAPI CustomerThreadFunc(PVOID pvParam)
{
customer<int> *c = (customer<int> *)pvParam ;

DWORD dwResult = 0;

c->CustomerObject() ;

return(dwResult);
}
atuyu 2011-10-31
  • 打赏
  • 举报
回复
先收藏下
timingbob 2011-10-28
  • 打赏
  • 举报
回复
3楼 代码只能用VC++编译吧
z24324 2011-10-25
  • 打赏
  • 举报
回复
mark
思绪如云 2011-10-01
  • 打赏
  • 举报
回复
mark
lyhylex 2011-09-23
  • 打赏
  • 举报
回复
mark
miao6664659 2011-07-29
  • 打赏
  • 举报
回复
#include <windows.h>
#include <iostream.h>



DWORD WINAPI Fun1Proc(LPVOID lpParamter);
DWORD WINAPI Fun2Proc(LPVOID lpParamter);

int index=0;
int tickets=100;
HANDLE hMutex;
HANDLE hMutex2;

DWORD WINAPI Fun1Proc(LPVOID lpParamter)
{
hMutex2=CreateMutex(NULL,TRUE,"miao");
if(hMutex2)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"mutex2 is aleday exist Func1proc"<<endl;
}
else
{
cout<<"hMutex2 is created by fun1proc"<<endl;
}
}
while(true)
{
ReleaseMutex(hMutex2);
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex2,INFINITE);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell tickets:"<<tickets--<<endl;
// SetEvent(g_hEvent);
//The state of an auto-reset event object remains signaled until a single waiting thread is released,
//at which time the system automatically sets the state to nonsignaled.
//If no threads are waiting, the event object's state remains signaled.
}
else
{
// SetEvent(g_hEvent);
break;
}
ReleaseMutex(hMutex);
ReleaseMutex(hMutex2);
}
return 0;
}

DWORD WINAPI Fun2Proc(LPVOID lpParamter)
{
hMutex2=CreateMutex(NULL,TRUE,"miao");
if(hMutex2)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"Fun2 Mutex2 is already exist"<<endl;
}
else
{
cout<<"Mutex2 is created by func2proc"<<endl;
}
}
while(true)
{
ReleaseMutex(hMutex2);
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex2,INFINITE);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell tickets:"<<tickets--<<endl;
//SetEvent(g_hEvent);
}
else
{
// SetEvent(g_hEvent);
break;
}
ReleaseMutex(hMutex);
ReleaseMutex(hMutex2);
}
return 0;
}

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);
hMutex=CreateMutex(NULL,TRUE,"tickets");
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only one instance can run!"<<endl;
return;
}
WaitForSingleObject(hMutex,INFINITE);
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
Sleep(4000);
}
}
我感觉 得再加一把锁 才能保证运行的不出错
TOMCNC 2011-07-25
  • 打赏
  • 举报
回复
能搞个LUNUX上的吗??WINDOWS上的太没技术含量了。
songyijie1990 2011-07-11
  • 打赏
  • 举报
回复
学习中
加载更多回复(25)

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧