Problem in compiling Threadpool functions like QueUserWorkItem()
I would like to increase the number of threads on my video coded application with the help of threadpool functions. Here is what I have done until now. The problem is that I am unable to compile it.
Code:
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#define THREAD_SIZE 6
DWORD WINAPI lprint(LPVOID p);
struct node
{
int p,q;
};
typedef struct node* NODEPTR;
void main()
{
NODEPTR ptr[THREAD_SIZE];
DWORD th[THREAD_SIZE];
BOOL hth[THREAD_SIZE];
int i;
for(i=0;i<THREAD_SIZE;i++)
{
ptr[i]=(NODEPTR)malloc(sizeof(NODEPTR));
ptr[i]->p=i;
ptr[i]->q=i*1000;
//hth[i]=CreateThread(NULL,0,lprint,ptr[i],0,&th[i]);
hth[i]=QueueUserWorkItem(lprint,ptr[i],WT_EXECUTEDEFAULT);
//WT_EXECUTEINIOTHREAD
}
WaitForMultipleObjects(THREAD_SIZE, hth , TRUE, INFINITE);
for(i=0;i<THREAD_SIZE;i++)
{
CloseHandle(hth[i]);
}
getch();
}
DWORD WINAPI lprint(LPVOID l)
{
NODEPTR s1=(NODEPTR) l;
printf("%d %d\n",s1->p,s1->q);
return 0;
}
It gives me an error message which states "error C3861: 'QueueUserWorkItem': identifier not found. Is there someone who can explain me this and provide a solution to it?
Re: Problem in compiling Threadpool functions like QueUserWorkItem()
A compiler reads the code from top to bottom. It first looks for the header files and then imports all the classes and functions that are included in it and then directly goes into your main(). According to your code, 'QueueUserWorkItem' is undefined. Do you have its definition included in your windows.h file? If it isn't then you have to define it explicitly before it is being used.
Re: Problem in compiling Threadpool functions like QueUserWorkItem()
I think you are having an old SDK and so QueUserWorkItem() is left unidentified. The windows.h file doesn't have its definition. You need to install the latest one and thus your file will be changed to winbase.h. If you don't want to install the new SDK then simply insert the below statement on the first line of your main():
Code:
QueueUserWorkItem(func, 0, 0);
Re: Problem in compiling Threadpool functions like QueUserWorkItem()
The fact that you are trying to do is calling a function which is undefined or undeclared. Check your header files and see if it exists somewhere or not. If it isn't then declare it in your code and then try to compile the code.
Re: Problem in compiling Threadpool functions like QueUserWorkItem()
Ok thanks for your input. My windows.h file had its definition but the only thing was that it declaration was commented (//) and that was what creating a problem for me. I removed those comments sign and now my code is working. Anyways thanks everyone. One more question that I would like to ask is that I have read somewhere few days about WT_EXECUTELONGFUNCTION flag of QueueUserWorkItem function. But I didn't understood much about its concept. Can you all help me understanding what it is exactly and what is the advantage of using it?
Re: Problem in compiling Threadpool functions like QueUserWorkItem()
First let get to the basic concept like why we use QueueUserWorkItem function. It is used to delay a work item to execute. But now the point comes like for how long it should wait, whether it should create a new thread or not and how to manage this. Here comes into the picture the WT_EXECUTELONGFUNCTION flag. This flag helps the thread pool to decide whether to add a new thread to the pool; it forces the thread pool to create a new thread if all of the threads in the pool are busy. So if you have 10,000 work items in the queue at the same time, 10,000 threads will be added to the thread pool. If you do not want 10,000 threads to be created, then you must space out the calls to QueueUserWorkItem so that some work items get a chance to complete.
Re: Problem in compiling Threadpool functions like QueUserWorkItem()
WT_EXECUTELONGFUNCTION (0x00000010) is the callback function to perform a long wait. This flag helps the system to decide if it should create a new thread. In other words, the thread pool uses this flag to decide whether it should create a new thread or wait for an existing one to finish. If you use WT_EXECUTELONGFUNCTION flag then the system will be very resistive to create a new thread, and this is the main reason that you will have more threads than CPU's.