• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 654
  • Last Modified:

Thread Error

I typed in a code from a book. I know its incomplete but it gives me compile error.
The code is:
#include<windows.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
static int runFlag=TRUE;
void main(int argc,char *argv[]){
      unsigned int runTime;
   SYSTEMTIME now;
   WORD stopTimeMinute, stopTimeSecond;
   //Initializing parameters for the thread
  DWORD WINAPI myfunc(LPVOID);
   LPSECURITY_ATTRIBUTES lpThreadAttributes = NULL;
   DWORD stackSIZE = 0;
   int theArg;
   DWORD dwCreationFlags = 0;
   DWORD targetThreadID;

   unsigned long _beginthread(
   (void *) lpThreadAttributes ,
   (unsigned) stackSize,
   (unsigned (_stdcall *)(void *)) myFunc,
   (void *) &theArg,
   (unsigned) dwCreationFlags,
   (unsigned *) &targetThreadID
   );

   //Get command line argument, N
   int N;
   if(argc!=2){
         fprintf(stderr,"Usage: mthread <value of N>");
      exit(0);
   }
   N=(int)argv[1];
   GetSystemTime(&now);
   printf("mthread:Suite starting at system time %d:%d:%  d\n",now.wHour,now.wMinute,now.wSecond);
   stopTimeSecond = (now.wSecond + (WORD) runTime) % 60;
   stopTimeMinute = now.wMinute + (now.wSecond + (WORD) runTime) / 60;

   for(int i=0;i<N;i++){

         Sleep(100);
   }

   while(runFlag){
      GetSystemTime(&now);
      if((now.wMinute >= stopTimeMinute) && (now.wSecond >= stopTimeSecond))
            runFlag = FALSE;
      Sleep(1000);
   }
   Sleep(5000);
}

The error are:

Error:  mthread.cpp(23,33):Cannot convert 'void *' to 'unsigned long'
Error:  mthread.cpp(23,33):) expected.

These errors refer to:
unsigned long _beginthread(
   (void *) lpThreadAttributes ,
   (unsigned) stackSize,
   (unsigned (_stdcall *)(void *)) myFunc,
   (void *) &theArg,
   (unsigned) dwCreationFlags,
   (unsigned *) &targetThreadID
   );
I also used an example code from BOrland c++ help file which makes use of _beginThreadNT(), but I get a compile time error saying undefined function _beginThreadNT()

0
jaspreet900
Asked:
jaspreet900
  • 5
  • 3
1 Solution
 
SteHCommented:
The main problem I see is that you declare myFunc inside main.

It should be

DWORD WINAPI myFunc(LPVOID) {
 // here goes the code
}

int main (...) {
// and here you can use the function address to myFunc    

}
0
 
SteHCommented:
And you define
myfunc
but use
myFunc
C/C++ are case sensitive.

_beginthread only has 3 parameters, you give 6 which looks more like the use of
_beginthreadex
0
 
SteHCommented:
Found some more errors to remove the compiler erros you stated:
#include <process.h> // defines _beginthread, _beginthreadex

unsigned long ulRet = _beginthread(
   (void *) lpThreadAttributes ,
   (unsigned) stackSize,
   (unsigned (_stdcall *)(void *)) myFunc,
   (void *) &theArg,
   (unsigned) dwCreationFlags,
   (unsigned *) &targetThreadID
   );

The function returns an unsigned long value. You can either call it
_beginthread(
   (void *) lpThreadAttributes ,
   (unsigned) stackSize,
   (unsigned (_stdcall *)(void *)) myFunc,
   (void *) &theArg,
   (unsigned) dwCreationFlags,
   (unsigned *) &targetThreadID
   );
and forget about the return value or you need to assign the return value to a variable of type unsigned long.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
SteHCommented:
And be aware that you need to compile with cRT options set to multithreading. And depending on the compiler the conversion from DWORD to unsigned int might give problems:
myFunc is defined now as
unsigned long    (__stdcall*)      (void*)
DWORD             WINAPI           LPVOID
and _beginthreadex expects as third parameter a function of the following type:
unsigned int (__stdcall*) (void*)
Either you define your function as
unsigned int WINAPI myFunc (LPVOID);
or you need to cast it to
(unsigned int (__stdcall*) (void*)) myFunc,  // note the two underscores before stdcall!!!
0
 
jaspreet900Author Commented:
unsigned long ulRet= _beginthread(
   (void *) lpThreadAttributes ,
   (unsigned) stackSIZE,
   (unsigned (_stdcall *)(void *)) myfunc,
   (void *) &theArg,
   (unsigned) dwCreationFlags,
   (unsigned *) &targetThreadID
   );
This is what I have now.
It gives me a compile time error saying _begintread is undefined function. I am using Borland C++.
After having gone through the help files I tries using _beginthreadNT, it still gives the same error.
Even an example provided in the help section doesn' compile. It gives the same error that the function is undefined.
0
 
jaspreet900Author Commented:
Hi,
I am still waiting for a reply. I tried running the code under Dev C++ it compiles perfectly, just any idea what the function "myFunc" supposed to contain.
0
 
SteHCommented:
The function myFunc should contain code to execute in a seperate thread. Normally that is CPU intensive code like FFT computations to keep the GUI responsive. Or for server applications often a thread is used to communicate with a single client; each new client gets handled in a new thread.
0
 
jaspreet900Author Commented:
Thanks, for your reply. I did figure it out. But your previous replies helped tremendously.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now