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
Solved

Thread Error

Posted on 2004-10-08
8
637 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

790 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