Solved

Thread Error

Posted on 2004-10-08
8
638 Views
Last Modified: 2012-05-05
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
Comment
Question by:jaspreet900
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12257530
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
 
LVL 13

Expert Comment

by:SteH
ID: 12257545
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
 
LVL 13

Accepted Solution

by:
SteH earned 40 total points
ID: 12257588
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:SteH
ID: 12257636
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
 

Author Comment

by:jaspreet900
ID: 12262584
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
 

Author Comment

by:jaspreet900
ID: 12270082
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
 
LVL 13

Expert Comment

by:SteH
ID: 12274646
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
 

Author Comment

by:jaspreet900
ID: 12277656
Thanks, for your reply. I did figure it out. But your previous replies helped tremendously.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question