多线程

toadzw 2008-10-31 05:04:20
c++中如何才能使用多线程(非VC)
...全文
91 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cattycat 2008-11-01
  • 打赏
  • 举报
回复
楼上的不错。定义函数用DWORD WINAPI func(LPVOID),然后传递给CreateThread就行。
dwen20 2008-11-01
  • 打赏
  • 举报
回复
The CreateThread function is the Windows function that creates a thread. However, if you are writing C/C++ code, you should never call CreateThread. Instead, you should use the Microsoft C++ run-time library function _beginthreadex. If you o not use Microsoft's C++ compiler, your compiler vendor will have its own alternative to CreateThread. Whatever this alternative is, you must use it. Later in this chapter, I'll explain what _beginthreadex does and why it is so important

CreatTread的使用
DWORD WINAPI FirstThread(PVOID pvParam) {
// Initialize a stack-based variable
int x = 0;
DWORD dwThreadID;

// Create a new thread.
HANDLE hThread = CreateThread(NULL, 0, SecondThread, (PVOID) &x,
0, &dwThreadID);

// We don't reference the new thread anymore,
// so close our handle to it.
CloseHandle(hThread);

// Our thread is done.
// BUG: our stack will be destroyed, but
// SecondThread might try to access it.
return(0);
}


DWORD WINAPI SecondThread(PVOID pvParam) {
// Do some lengthy processing here. ... //
Attempt to access the variable on FirstThread's stack.
// NOTE: This may cause an access violation – it depends on timing!
* ((int *) pvParam) = 5; ... return(0);
}


_beginthreadex原型
unsigned long _beginthreadex(
void *security,
unsigned stack_size,
unsigned (*start_address)(void *),
void *arglist,
unsigned initflag,
unsigned *thrdaddr);

luojc714 2008-11-01
  • 打赏
  • 举报
回复
C++本身没什么多线程的东西吧!
多线程是操作系统的东西,所以不同OS下,可能有不同的API库函数来支持吧!
Win下:CreateThread(...)
luojc714 2008-11-01
  • 打赏
  • 举报
回复
C++本身没什么多线程的东西吧!
多线程是操作系统的东西,所以不同OS下,可能有不同的API库函数来支持吧!
Win下:CreateThread(...)
sunyg0123 2008-11-01
  • 打赏
  • 举报
回复
又长知识了
sms88 2008-10-31
  • 打赏
  • 举报
回复
Createthread
查MSDN
toadzw 2008-10-31
  • 打赏
  • 举报
回复
give me an example ....please
xhs_lh04 2008-10-31
  • 打赏
  • 举报
回复
嗯使用POSIX线程库吧
simonyuan 2008-10-31
  • 打赏
  • 举报
回复
用_beginthread
hhyttppd 2008-10-31
  • 打赏
  • 举报
回复
C++不直接支持多线程,包括同步这些概念都找不到语言本身的支持。
可以使用第三方库posix threads?
wuyu637 2008-10-31
  • 打赏
  • 举报
回复
c++不支持多线程,

支持多线程的操作系统的API或者其他的多线程类库


win下:

Createthread
http://msdn.microsoft.com/en-us/library/ms682453(VS.85).aspx



linux下:
prthread_create()

64,652

社区成员

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

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