Trouble with _beginthread's third option (arglist)

_beginthread won't seem to run without any args to function, even

Page 1 of 1

10 Replies - 4510 Views - Last Post: 17 September 2009 - 12:33 PM Rate Topic: -----

#1 brycetron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-September 09

Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 12:37 AM

I am a beginner, so please be gentle ;-)

This is a program to test stepper motors. The second thread is to check for input to change speed, direction in real-time.

The issue is with
_beginthread(CheckKey, 0 , _ArgList);

Don't know what to put for _ArgList, since it would be void for CheckKey(void)

// Stepper.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "conio.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <windows.h>
#include <process.h>
#include <iostream>
#include <sstream>
using namespace std;
#define Data		0x378	//Lpt1 Data register
#define Status		Data+1	//Lpt1 Status Register
#define Control		Data+2	//Lpt1 Control Register

// ----Prototypes of Inp and Outp are contained in sdafx.h---

short _stdcall Inp32(short PortAddress);
void _stdcall Out32(short PortAddress, short data);

//--------------------------------

void CheckKey(void);//function to check for user input

int kb = 1;
int delay = 400;//atoi(argv[2]);
char direction = 'f';//argv[1];
int speed = 1;
int kbv = 0;
int steps = 25;//atoi(argv[3]);
BOOL run = true;
   

int _main(int argc, _TCHAR* argv[])
{

	Out32(Data,0x0000);//Clear Parallel port
   

	_beginthread(CheckKey,0,NULL);//threaded function to check for user input

	while(run)
	{
		if(direction = 'f')//Move stepper forward
		{
			Sleep(delay/4);
			Out32(Data,0x9);
			Sleep(delay/4);
			Out32(Data,0x5);
			Sleep(delay/4);
			Out32(Data,0x6);
			Sleep(delay/4);
			Out32(Data,0xa);
		}else if(direction = 'r')//Move stepper in reverse
		{
			Sleep(delay/4);
			Out32(Data,0xa);
			Sleep(delay/4);
			Out32(Data,0x6);
			Sleep(delay/4);
			Out32(Data,0x5);
			Sleep(delay/4);
			Out32(Data,0x9);
		   
		}
	   

	}
   
	Out32(Data,0x0000);//Clear Parallel port
   
	return 0;
}

	void CheckKey(void)
	{

		cout << "\nDirection: " << direction << "\nSpeed is: " << speed << "\n";

		while (_getch() != 'q')//_kbhit != 0)
		{

			if(_getch() == '+' && delay > 10)
			{
				delay -= 10;
				speed++;
				cout << "\nSpeed is: " << speed;
			}else if(_getch() == '-' && speed > 1)
			{
				delay += 10;
				speed--;
				cout << "\nSpeed is: " << speed;
			}else if (_getch() == 'f')
			{
				direction = 'f';
				cout<< "\nDirection is: " << direction;
			}else if(_getch() == 'r')
			{
				direction = 'r';
				cout<< "\nDirection is: " << direction;
			}else
			{
				kb = 1;		
			}
		   
			Out32(Data,0x0000);
			run = 0;
			_endthread();
		}
	   
	}



Compileing gives this error:

: error C2664: '_beginthread'
: cannot convert parameter 1 from 'void (__cdecl *)(void)' to 'void (__cdecl *)(void *)'
1> None of the functions with this name in scope match the target type

This post has been edited by brycetron: 17 September 2009 - 09:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with _beginthread's third option (arglist)

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 05:38 AM

Just use NULL or 0.
Was This Post Helpful? 0
  • +
  • -

#3 brycetron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-September 09

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 08:33 AM

View PostJackOfAllTrades, on 17 Sep, 2009 - 04:38 AM, said:

Just use NULL or 0.


Thank you for the quick reply!

This is the output I get for NULL as arg:
: error C2664
: '_beginthread'
: cannot convert parameter 1 from 'void (__cdecl *)(void)' to 'void (__cdecl *)(void *)'
1> None of the functions with this name in scope match the target type

This is the output I get for 0 as arg:
: error C2664
: '_beginthread' : cannot convert parameter 1 from 'void (__cdecl *)(void)' to 'void (__cdecl *)(void *)'
1> None of the functions with this name in scope match the target type

It seems like there is some sort of data type expected, I just don't know what!

This post has been edited by brycetron: 17 September 2009 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 09:46 AM

The compiler error is telling you that it expects the function passed in the first argument to _beginthread to have the signature:
void *funcname(void *)


Your function has the signature:
void CheckKey(void)


Can you spot the two differences there?
Was This Post Helpful? 0
  • +
  • -

#5 brycetron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-September 09

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 10:00 AM

View PostJackOfAllTrades, on 17 Sep, 2009 - 08:46 AM, said:

The compiler error is telling you that it expects the function passed in the first argument to _beginthread to have the signature:
void *funcname(void *)


Your function has the signature:
void CheckKey(void)


Can you spot the two differences there?


The asterisk defines a pointer right? So the address of the function (*function) is passed. But void * I don't understand. Points to empty space?
Was This Post Helpful? 0
  • +
  • -

#6 brycetron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-September 09

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 10:10 AM

Is it a problem with my function definition or prototype?
Was This Post Helpful? 0
  • +
  • -

#7 tobix10  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 06-April 09

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 10:13 AM

both
Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 10:17 AM

Sorry, you're correct, because passing the function name passes the address of the function, that pointer is taken care of. But check out the example code on this page.

A void pointer is just a pointer, or what is called an opaque pointer. It can point to *anything*, so this allows you to pass whatever you want to your thread function. So if you had a struct like this:
struct person_rec
{
    int id;
    char *name;
};

and you wanted to pass that to the thread function, you could, like so:
struct person_rec r;
r.id = 1;
r.name = malloc(strlen("My Name") + 1);
strcpy(r.name, "My Name");

_beginthread(PrintRecord, (void *)&r);


Then, in the PrintRecord function, you know what's coming so you recreate it:
void PrintRecord(void *arg)
{
    struct person_rec *r = NULL;
    if (arg)
    {
        r = (struct person_rec *)arg;
        printf("ID: %d, Name: %s\n", r->id, r->name);
    }
}

Was This Post Helpful? 1
  • +
  • -

#9 brycetron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-September 09

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 11:05 AM

Okay, so I gave a dummy value to CheckKey function. That was all it needed ;; I think ;;.

Now I am getting some errors, this about an unreferenced symbol:

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

: fatal error LNK1120: 1 unresolved externals

I there a specific include or lib that I am missing? Both those errors appear to be caused by one issue.

// Stepper.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <iostream>
#include <sstream>
using namespace std;

#define Data		0x378	//Lpt1 Data register
#define Status		Data+1	//Lpt1 Status Register
#define Control		Data+2	//Lpt1 Control Register

// ----Prototypes of Inp and Outp are contained in sdafx.h---

short _stdcall Inp32(short PortAddress);
void _stdcall Out32(short PortAddress, short data);

//--------------------------------

int kb = 1;
int delay = 400;//atoi(argv[2]);
char direction = 'f';//argv[1];
int speed = 1;
int kbv = 0;
int steps = 25;//atoi(argv[3]);
BOOL run = true;

void CheckKey(void *dummy);//function to check for user input
   

int _main(int argc, _TCHAR* argv[])
{

	Out32(Data,0x0000);//Clear Parallel port
   

	_beginthread(CheckKey,0,NULL);//threaded function to check for user input

	while(run)
	{
		if(direction = 'f')//Move stepper forward
		{
			Sleep(delay/4);
			Out32(Data,0x9);
			Sleep(delay/4);
			Out32(Data,0x5);
			Sleep(delay/4);
			Out32(Data,0x6);
			Sleep(delay/4);
			Out32(Data,0xa);
		}else if(direction = 'r')//Move stepper in reverse
		{
			Sleep(delay/4);
			Out32(Data,0xa);
			Sleep(delay/4);
			Out32(Data,0x6);
			Sleep(delay/4);
			Out32(Data,0x5);
			Sleep(delay/4);
			Out32(Data,0x9);
		   
		}
	   

	}
   
	Out32(Data,0x0000);//Clear Parallel port
   
	return 0;
}

	void CheckKey(void *dummy)
	{

		cout << "\nDirection: " << direction << "\nSpeed is: " << speed << "\n";

		while (_getch() != 'q')//_kbhit != 0)
		{

			if(_getch() == '+' && delay > 10)
			{
				delay -= 10;
				speed++;
				cout << "\nSpeed is: " << speed;
			}else if(_getch() == '-' && speed > 1)
			{
				delay += 10;
				speed--;
				cout << "\nSpeed is: " << speed;
			}else if (_getch() == 'f')
			{
				direction = 'f';
				cout<< "\nDirection is: " << direction;
			}else if(_getch() == 'r')
			{
				direction = 'r';
				cout<< "\nDirection is: " << direction;
			}else
			{
				kb = 1;		
			}
		   
			Out32(Data,0x0000);
			run = 0;
			_endthread();
		}
	   
	}


This post has been edited by brycetron: 17 September 2009 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 12:27 PM

It should be int main, not int _main.
Was This Post Helpful? 1
  • +
  • -

#11 brycetron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-September 09

Re: Trouble with _beginthread's third option (arglist)

Posted 17 September 2009 - 12:33 PM

Der.. That was pretty obvious, thank you SO much for all your help. I will try a couple other projects to get a better feel for multithread, as it seems very useful. I read about the boost libraries and may also give them a try. Thanks again!

Quick note: Visual Studio 2008 seems to add that underscore by default, which is weird and a bit confusing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1