Solved

Thread Error

Posted on 2004-10-08
8
635 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 155
object modeling - load and save functions 5 54
How to split this in C++ 4 77
Header of docx file 17 58
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  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 …
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.
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now