0
votes

I am trying to use a threading library which has a thread start function defined like this:

int vg_thread_create(VGThreadFunction func, 
  void *arg, 
  VGThreadDescriptor *tid,
  void *stack, 
  size_t stack_size,
  long priority,
  int flags );

And VGThreadFunction is the following typedef:

typedef void* (*VGThreadFunction) ( void* )

For the sake of argument I have a function like this:

void doit() {
  cout << "Woohoo printing stuff in new thread\n";
}

The signature of this function is not void* func(void*) - am I forced to use such a function signature?

If I try:

VGThreadFunction testfunc = &doit;

I get

error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(void)' to 'VGThreadFunction'

How do I use vg_thread_create?

EDIT

I tried this:

void* doit(void* donothingvar) {
  cout << "printing stuff in new thread\n";
  return donothingvar;
}

then in a function:

VGThreadFunction testfunc = (VGThreadFunction)doit;

int ret = vg_thread_create(testfunc, 0, 0, 0, 0, 0, 0);

I then get an access violation when calling vg_thread_create.

If I step into the function it actually crashes when it calls _beginthreadex

Unhandled exception at 0x0040f5d3 in threadtest.exe: 0xC0000005: Access violation  writing location 0x00000000.

Any ideas?

By stepping into function I worked out I had to pass in a valid pointer - passing zero resulted in a null dereference. sorry, a bit specific.

1

1 Answers

6
votes

Yes, the function signature has to match. Just put a dummy parameter on your function.