1

I am trying to create a scheduler, and when creating a thread, it gives the following error:

argument of type "DWORD (*)(LPVOID lpParameter)" is incompatible with parameter of type "LPTHREAD_START_ROUTINE".

I made the thread function static as suggested in other questions and it still gives the same error. Any solution or pointers would be appreciated.

This is the relevant part of my code:

#include "stdafx.h"
#include <string>
#include <windows.h>
#include <vector>
#include <stdio.h>
#include <thread>

using namespace std;

struct process {
    int PID = 0;
    int burstTime;
    int arrivalTime;
    int priority;
};

vector<process> allProcesses;
process myProcess;

vector<HANDLE> handles;
vector<DWORD> dwords;

static DWORD myThread(LPVOID lpParameter)
{
    //execute my thread
}

void newArrival()
{
    DWORD tempThreadID;
    dwords.push_back(tempThreadID);

    ////error happens here at myThread
    handles.push_back(CreateThread(0, 0, myThread, &allProcesses[0], 0, &dwords[dwords.size() - 1]));

    //do even more stuff
}

void scheduler()
{
    //do some other stuff
    newArrival();
    //do more stuff
}

int main()
{
    //do some stuff

    myProcess.arrivalTime = 1000;
    myProcess.burstTime = 2500;
    myProcess.priority = 90;

    allProcesses = {myProcess};

    thread scheduler(scheduler);

    system("PAUSE > null");
    return 0;
}
ankit
  • 125
  • 4
  • 13
  • 3
    It depends on your compiler settings and target architecture, but you are likely missing ``__stdcall`` on the ``myThread`` function. You only need to make it 'static' if it was a member method (which would imply a ``*this`` first parameter). Usually you use the ``WINAPI`` macro. Try ``DWORD WINAPI myThread(LPVOID lpParameter)``. – Chuck Walbourn Mar 18 '18 at 06:26
  • You, Sir @ChuckWalbourn, are a savior! Removing the static and adding WINAPI(equivalent of __stdcall) worked like a charm! – ankit Mar 18 '18 at 06:33
  • @ChuckWalbourn `static` is not just for class methods. Using `static` on a global function causes it to have internal linkage and not be accessible to other translation units via `extern`. In a simple 1-unit example like this, it doesn't make a difference. In a production app with many units, it can make a difference in speeding up linking and protecting private code. – Remy Lebeau Mar 18 '18 at 16:43
  • This is very, very basic stuff... This is how you create a thread: CreateThread(0, 0, (LPTHREAD_START_ROUTINE)myThread, &allProcesses[0], 0, &dwords[dwords.size() - 1]) ... – Mecanik Mar 23 '18 at 09:20
  • @NorbertBoros error was in function declaration, not in the function call. As , chuck mentioned, there has to be a __stcall in the declaration – ankit Mar 25 '18 at 22:32

0 Answers0