关于多线程的问题

minoriz 2006-03-24 10:32:56
上次在这里问过一个多线程的问题,已经结了

代码如下:
#include "stdafx.h"
#include <iostream>

#include <windows.h>
#include <stdio.h>
#include <process.h>

unsigned Counter;
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
printf( "In second thread...\n" );

while ( Counter < 1000000 )
Counter++;

_endthreadex( 0 );
return 0;
}

int main()
{
HANDLE hThread;
unsigned threadID;

printf( "Creating second thread...\n" );

// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );

// Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
WaitForSingleObject( hThread, INFINITE );
printf( "Counter should be 1000000; it is-> %d\n", Counter );
// Destroy the thread object.
CloseHandle( hThread );
}

这个程序我在VS 2005中编译通过,能运行,但因为某些要求,现在必须在VS 2003中运行,但是VS 2003说找不到_beginthreadex和_endthreadex,请问这个两个函数是在那个头文件里面的,为什么VS 2003和VS 2005不同,多谢。

另外大家有没有用过LOG4CPLUS,这个在多线程的情况下会有问题么?
...全文
260 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
roger_77 2006-05-20
  • 打赏
  • 举报
回复
log4cpp没有多线程保证. 这个不清楚
那可以考虑log4cXX
请看:
http://logging.apache.org/log4cxx/manual/Introduction.html
This is usually the case for multithreaded applications and distributed applications at large.

吃狼的豆腐 2006-05-19
  • 打赏
  • 举报
回复
这个程序我在VS 2005中编译通过,能运行,但因为某些要求,现在必须在VS 2003中运行,但是VS 2003说找不到_beginthreadex和_endthreadex,请问这个两个函数是在那个头文件里面的,为什么VS 2003和VS 2005不同,多谢。

另外大家有没有用过LOG4CPLUS,这个在多线程的情况下会有问题么?
_____________________________________________________________
这两个函数是windows95添的,现在的系统应该都支持的,你在项目属性里面换成,使用多线程c runtime类库,应该就没有问题了,2005默认选择的是多线程版本,vc6.0是默认单线程,.net2003不太清楚,应该也是这个问题吧
goke 2006-05-19
  • 打赏
  • 举报
回复
Sorry,
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, \
(void)*demo, 0, &threadID);
Should be
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, \
(void *)&demo, 0, &threadID);
goke 2006-05-19
  • 打赏
  • 举报
回复
1. Declaration of _beginthreadex and _endthreadex is in "process.h".
2. If you need more than 1 parameter you can define a struct. Follow is an
example:
typedef struct {
int a;
int **b;
char *c;
}DEMO_STRUCT;

DEMO_STRUCT demo;
demo.a = 1234;
demo.c = NULL;
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, \
(void)*demo, 0, &threadID);

In the definition of function SecondThreadFunc, you may process
the parameter like below:
...........
DEMO_STRUCT *var = (DEMO_STRUCT *)pArguments;
printf("%d %s\n",var->a, var->c);
...........
纪俊 2006-03-29
  • 打赏
  • 举报
回复
没碰到过
帮顶
smartduck 2006-03-25
  • 打赏
  • 举报
回复
1.能不能改为用CreateThread/ExitThread, 应该都支持吧.
2.log4cpp没有多线程保证. 但是在实际的使用中, 好像在多线程中也没有发现问题.
3.如果需要传递多个参数, 那就自己定义一个类嘛
minoriz 2006-03-24
  • 打赏
  • 举报
回复
另外还有个问题,希望大家能帮忙。参数是通过pArguments传入的。但假设我希望传递两个参数,比如一个int**,一个int,有没有办法可以做到这一点?

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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