阅读:14818回复:18
_beginthread()的用法,请多指教!
请大侠多多指教!谢谢!
_beginthread( void( __cdecl *start_address )( void * ), unsigned stack_size, void *arglist ); 第一个参数‘start_address’就是要调用的那个函数名吗? 第三个参数‘arglist’是一个自变量列表是什么意思呢? 如果要调用的函数有好几个参数,把那些参数设为全局变量,再把‘arglist’设成NULL可以吗? 如果不想把那些参数设为全局变量,该怎么传呢? 例如想调用的函数是:void PCI9054_ReadFrRam2 (PCI9054_HANDLE hPCI9054, PCI9054_ADDR addrSpace, DWORD dwOffset,FILE *fp) [编辑 - 11/15/04 by zsh_7769] |
|
沙发#
发布于:2004-11-16 08:28
把一些列变量构造一个struct,然后传递该指针
|
|
|
板凳#
发布于:2004-11-16 09:19
构造一个struct如下:
typedef struct { PCI9054_HANDLE hPCI9054; PCI9054_ADDR addrSpace; DWORD dwOffset; FILE *fp; } THREAD; 调用如下: _beginthread( PCI9054_ReadFrRam2,0,&THREAD); 但编译错误,呵呵,该怎么处理了? |
|
地板#
发布于:2004-11-16 09:35
构造一个struct如下: 出现的是什么错误????? 不过你得先声明一个结构变量后再用吧 至少得 THREAD hRead; _beginthread( PCI9054_ReadFrRam2,0,&hRead); 这样吧 |
|
地下室#
发布于:2004-11-16 09:55
要这样调用:_beginthread( PCI9054_ReadFrRam2,0,(void*)&THREAD);
|
|
5楼#
发布于:2004-11-16 19:28
/* BEGTHRD.C illustrates multiple threads using functions:
* * _beginthread _endthread * * * This program requires the multithreaded library. For example, * compile with the following command line: * CL /MT /D "_X86_" BEGTHRD.C * * If you are using the Visual C++ development environment, select the * Multi-Threaded runtime library in the compiler Project Settings * dialog box. * */ #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #include <stddef.h> #include <stdlib.h> #include <conio.h> void Bounce( void *ch ); void CheckKey( void *dummy ); /* GetRandom returns a random integer between min and max. */ #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min)) BOOL repeat = TRUE; /* Global repeat flag and video variable */ HANDLE hStdOut; /* Handle for console window */ CONSOLE_SCREEN_BUFFER_INFO csbi; /* Console information structure */ void main() { CHAR ch = 'A'; hStdOut = GetStdHandle( STD_OUTPUT_HANDLE ); /* Get display screen's text row and column information. */ GetConsoleScreenBufferInfo( hStdOut, &csbi ); /* Launch CheckKey thread to check for terminating keystroke. */ _beginthread( CheckKey, 0, NULL ); /* Loop until CheckKey terminates program. */ while( repeat ) { /* On first loops, launch character threads. */ _beginthread( Bounce, 0, (void *) (ch++) ); /* Wait one second between loops. */ Sleep( 1000L ); } } /* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */ void CheckKey( void *dummy ) { _getch(); repeat = 0; /* _endthread implied */ } /* Bounce - Thread to create and and control a colored letter that moves * around on the screen. * * Params: ch - the letter to be moved */ void Bounce( void *ch ) { /* Generate letter and color attribute from thread argument. */ char blankcell = 0x20; char blockcell = (char) ch; BOOL first = TRUE; COORD oldcoord, newcoord; DWORD result; /* Seed random number generator and get initial location. */ srand( _threadid ); newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 ); newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 ); while( repeat ) { /* Pause between loops. */ Sleep( 100L ); /* Blank out our old position on the screen, and draw new letter. */ if( first ) first = FALSE; else WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result ); WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result ); /* Increment the coordinate for next placement of the block. */ oldcoord.X = newcoord.X; oldcoord.Y = newcoord.Y; newcoord.X += GetRandom( -1, 1 ); newcoord.Y += GetRandom( -1, 1 ); /* Correct placement (and beep) if about to go off the screen. */ if( newcoord.X < 0 ) newcoord.X = 1; else if( newcoord.X == csbi.dwSize.X ) newcoord.X = csbi.dwSize.X - 2; else if( newcoord.Y < 0 ) newcoord.Y = 1; else if( newcoord.Y == csbi.dwSize.Y ) newcoord.Y = csbi.dwSize.Y - 2; /* If not at a screen border, continue, otherwise beep. */ else continue; Beep( ((char) ch - 'A') * 100, 175 ); } /* _endthread given to terminate */ _endthread(); } |
|
6楼#
发布于:2004-11-17 09:10
编译出错:error C2664: '_beginthread' : cannot convert parameter 1 from 'void (struct PCI9054_STRUCT *,unsigned long,unsigned long,struct _iobuf *)' to 'void (__cdecl *)(void *)'
typedef struct PCI9054_STRUCT *PCI9054_HANDLE; 而 enum { PCI9054_AD_BAR0 = AD_PCI_BAR0, PCI9054_AD_BAR1 = AD_PCI_BAR1, PCI9054_AD_BAR2 = AD_PCI_BAR2, PCI9054_AD_BAR3 = AD_PCI_BAR3, PCI9054_AD_BAR4 = AD_PCI_BAR4, PCI9054_AD_BAR5 = AD_PCI_BAR5, PCI9054_AD_EPROM = AD_PCI_BAR_EPROM, }; typedef DWORD PCI9054_ADDR; 呵呵,不好意思,昨天没把 PCI9054_ADDR和PCI9054_HANDLE说清楚。 |
|
7楼#
发布于:2004-11-17 09:30
zsh_7769:
问题解决了没有? |
|
|
8楼#
发布于:2004-11-17 09:33
还没呢?就是你前一个帖子的问题
|
|
9楼#
发布于:2004-11-17 10:10
zsh_7769:
还没解决,kao,i服了you :) typedef struct __ThreadParam { PCI9054_STRUCT ps; unsigned long ul1; unsigned long ul2; struct _iobuf *pBuf; }ThreadParam, *PThreadParam; ThreadParam tp = {...}; void ThreadFunc(void *p) { PThreadParam ptp = (PThreadParam)p; return PCI9054_ReadFrRam2(ptp->ps, ...); } __BeginThread(ThreadFunc, 0, &tp); OK? |
|
|
10楼#
发布于:2004-11-17 10:53
maqian:
呵呵,不好意思,我的水平确实太臭,多谢多谢 |
|
11楼#
发布于:2004-11-17 11:02
zsh_7769:
不客气 |
|
|
12楼#
发布于:2004-11-17 11:24
maqian:
呵呵,不好意思,其实你上面写的那一段我看的不是很懂,能稍微讲解一下吗?多谢多谢! |
|
13楼#
发布于:2004-11-17 11:34
zsh_7769:
这么说,还没解决? cannot convert parameter 1 from 'void (struct PCI9054_STRUCT *,unsigned long,unsigned long,struct _iobuf *)' to 'void (__cdecl *)(void *)' 所以,要像我那样写。 OK? |
|
|
14楼#
发布于:2004-11-17 11:37
呵呵,只是不明白为什么这样写,虽然这样用可以解决,但不明白啊,想搞清楚
|
|
15楼#
发布于:2004-11-17 11:53
beginthread接受的线程函数(回调函数)规定只有一个参数(void *p),你给beginthread的函数有这么多的参数,那当然不行。所以,要将那些参数打包成一个指针,以符合'void (__cdecl *)(void *)的要求。
beginthread接受的线程函数也算是一个回调函数,只是它只被调用一次。 |
|
|
16楼#
发布于:2004-11-17 12:04
呵呵,先谢过啦,这段是什么意思呢?
ThreadParam tp = {...}; void ThreadFunc(void *p) { PThreadParam ptp = (PThreadParam)p; return PCI9054_ReadFrRam2(ptp->ps, ...); } |
|
17楼#
发布于:2004-11-17 12:31
呵呵,先谢过啦,这段是什么意思呢? 奇怪!线程函数居然返回一个空类型!! |
|
18楼#
发布于:2007-03-01 12:55
我也遇到上述问题
|
|
|