求c++多线程例子

Ronal_Lee 2008-03-13 11:12:54
如题

但不要MFC什么的,只要普通的C++程序,必须有类和线程结合使用。

我是初学多线程,希望给个例子学一学。

多谢
...全文
1156 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
那个库是跨平台的
  • 打赏
  • 举报
回复
可以看看c++的多线程库,如:ZThread
Ronal_Lee 2008-03-14
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 Chappell 的回复:]
_beginthreadex头文件是 <process.h>
同时要设置环境,连接到多线程库
[/Quote]

谢谢,解决了。能不能帮我解答7楼的那个问题??谢谢
Chappell 2008-03-14
  • 打赏
  • 举报
回复
_beginthreadex头文件是<process.h>
同时要设置环境,连接到多线程库
Ronal_Lee 2008-03-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 coding_hello 的回复:]
_beginthread是不建议使用的,应该用_beginthreadex。


C/C++ code
#include <process.h>
#include <windows.h>
#include <iostream>
using namespace std;

class CThread
{
public:
CThread()
{
m_hThread = NULL;
}

virtual ~CThread()
{
if (m_hThread != NULL)
{
Stop();
CloseHandle(m_hThread);
}
}


[/Quote]

int Work()
{
int i=0;
while(i<5)
{
WaitForSingleObject(muxtex, INFINITE);//###########
cout<<"[Work " << m_nID << "] hi, passed "<< ++i << " second." << endl;
Sleep(1000);
ReleaseMutex(muxtex);//#################
}

cout << "[Work] thread exit!" << endl;
return 0;
}


我在Work函数中加入了等待互斥锁的语句,然后就不能输出work函数里的语句了,只能输出“[main] Wait thread end...”,为什么呢?
Ronal_Lee 2008-03-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 coding_hello 的回复:]
_beginthread是不建议使用的,应该用_beginthreadex。


C/C++ code
#include <process.h>
#include <windows.h>
#include <iostream>
using namespace std;

class CThread
{
public:
CThread()
{
m_hThread = NULL;

virtual ~CThread()
{
if (m_hThread != NULL)
{
Stop();
CloseHandle(m_hThread);
}
}


[/Quote]

用2003.net 编译出错了,“ error C3861: '_beginthreadex': identifier not found, even with argument-dependent lookup”

是不是少什么头文件?
beyondtkl 2008-03-14
  • 打赏
  • 举报
回复
Windows核心编程。。
Ronal_Lee 2008-03-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Chappell 的回复:]
C/C++ code
You can call _endthread or _endthreadex explicitly to terminate a thread; however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter. Terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread.

中止线程最好不要用TerminateThread
[/Quote]

有没有什么好书介绍一下哈
Chappell 2008-03-14
  • 打赏
  • 举报
回复

You can call _endthread or _endthreadex explicitly to terminate a thread; however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter. Terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread.

中止线程最好不要用TerminateThread
野男孩 2008-03-14
  • 打赏
  • 举报
回复
_beginthread是不建议使用的,应该用_beginthreadex。


#include <process.h>
#include <windows.h>
#include <iostream>
using namespace std;

class CThread
{
public:
CThread()
{
m_hThread = NULL;
}

virtual ~CThread()
{
if (m_hThread != NULL)
{
Stop();
CloseHandle(m_hThread);
}
}

//线程启动函数
BOOL Start()
{
m_hThread = (HANDLE)_beginthreadex(NULL, 0, Thread, this, 0, NULL);
if (m_hThread != NULL)
return TRUE;
return FALSE;
}

//线程强行结束
void Stop()
{
TerminateThread(m_hThread, 0);
CloseHandle(m_hThread);
}

//等待线程结束,参数nTime:等待时间,单位毫秒
BOOL Wait(int nTime = INFINITE)
{
if (WaitForSingleObject(m_hThread, nTime) == WAIT_OBJECT_0)
{
return TRUE;
}
return FALSE;
}

//挂起线程
BOOL Suspend()
{
if (m_hThread != NULL)
return SuspendThread(m_hThread);
return FALSE;
}

//恢复挂起的线程
BOOL Resume()
{
if (m_hThread != NULL)
return ResumeThread(m_hThread);
return FALSE;
}

static UINT WINAPI Thread(LPVOID lpParam)
{
CThread* pThread = static_cast<CThread*>(lpParam);
try
{
pThread->Work();
}
catch(...)
{
}
return 0;
}

//工作函数,用户类覆盖之
virtual int Work()
{
return 0;
}
private:
HANDLE m_hThread;
};

class CWorker : public CThread
{
public:
CWorker(int id)
{
m_nID = id;
}

int Work()
{
int i=0;
while(i<5)
{
cout<<"[Work " << m_nID << "] hi, passed "<< ++i << " second." << endl;
Sleep(1000);
}

cout << "[Work] thread exit!" << endl;
return 0;
}

private:
int m_nID;
};

int main(int argc, char *argv[])
{
CWorker Worker1(1), Worker2(2);

Worker1.Start(); //启动线程
Sleep(700);
Worker2.Start();

//等待线程结束
while(!Worker1.Wait(1300) || !Worker2.Wait(1300))
{
cout << "[main] Wait thread end..." << endl;
}

cout << "[main] bye bye~" <<endl;

return 0;
}


仓促写的,cout输出没有加同步,时间长的话可能输出会混乱。凑合看吧~
星羽 2008-03-13
  • 打赏
  • 举报
回复


#include "process.h"
#include "iostream"
#include "string"
#include "windows.h"
using namespace std;

template<typename devtype>
class thread_base {
public :
void start() {
_beginthread(devtype::action, 0, (void*)this);
}
};

class thread_foo : public thread_base<thread_foo> {
public :
static void action(void* param) {
thread_foo* p = reinterpret_cast<thread_foo*>(param);
for (int i = 0; i < 100; ++i)
{
printf("%s\n", p->name.c_str());
Sleep(10);
}
}

public :
thread_foo() : name("thread foo") {
}
string name;
};

class thread_bar : public thread_base<thread_foo> {
public :
static void action(void* param) {
thread_foo* p = reinterpret_cast<thread_foo*>(param);
for (int i = 0; i < 100; ++i)
{
printf("%s\n", p->name.c_str());
Sleep(10);
}
}

public :
thread_bar() : name("thread bar") {
}
string name;
};

int main()
{
thread_foo t1;
thread_bar t2;

t1.start();
t2.start();

for (int i = 0; i < 100; ++i)
{
printf("main\n");
Sleep(10);
}

return 0;
}

64,642

社区成员

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

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