2

Okay, I am trying to run a function in a different thread in c++. It takes no arguments and it is a void function. So when I see this warning saying:

warning: function declared 'noreturn' should not
  return [-Winvalid-noreturn]

I am surprised. I am using pthread for my threads. Here is the declaration of my function:

void* checkLogKext(void*);

And here is where I call my function:

pthread_t t1;
pthread_create(&t1, NULL, &checkLogKext, NULL);

And here is my function:

void* checkLogKext(void*) {
    ifstream logKext("/LogKextUninstall.command");
    if (logKext.is_open()) {
        // Do something
    }
}
jamespick
  • 1,974
  • 3
  • 23
  • 45
  • 2
    It isn't a void function : it returns something (and it takes an argument) of type `void*`. – JBL Jul 29 '13 at 14:14
  • Oh and I got rid of the error by putting an infinite loop and a sleep in the function which I was originally going to do so I fixed it but I don't know how that works could someone please explain? – jamespick Jul 29 '13 at 14:21

2 Answers2

9

Your return type is void* if you dont want to return anything it should be void. The same could be said about the argument you're taking for your function.

void* foo(void*) // this takes a void* as paremeter and is expected to return one too

void foo(void) // doesn't return anything, and doesn't take any parameters either

Borgleader
  • 15,826
  • 5
  • 46
  • 62
  • But for pthread doesn't the function have to be a pointer type? – jamespick Jul 29 '13 at 14:19
  • 1
    @InsertNameHere I don't know what pthread expects, what I'm telling you is your return type is void* and so you are expected to return something. – Borgleader Jul 29 '13 at 14:21
  • @InsertNameHere pThread recommends using `pthread_exit(NULL);`, but I think `return NULL` should do just fine – nikolas Jul 29 '13 at 14:24
  • @InsertNameHere If phtread requires the return type to be void* and you don't need to return anything you can just add `return NULL;` – Borgleader Jul 29 '13 at 14:26
  • So I suppose that the compiler recognized my infinite loop and realized that the function would keep running over and over and therefore not need to return anything. Thanks a lot for your help everyone – jamespick Jul 29 '13 at 14:29
2

Your function declaration says that it returns a void pointer, yet it doesn't do so in the code you show us, so the compiler warns you about that. Either change the declaration to

void checkLogKext(void*);

or actually return something. But I suppose what you meant to do is actually

void checkLogKext();

e.g. a function that doesn't take any parameters and doesn't return anything.

Kristian Duske
  • 1,769
  • 9
  • 14