CreateThread problem

Below's the code that creates win threads and calls function "processStressThreads"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
BOOL createStressThreads()
{
    BOOL bReturn = FALSE;
    DWORD dwThreadId = 0;

    //
    // Start the threads
    //
    for(int i=1; i<=MAX_THREADS; ++i)
    {
        printf("Beginning Thread %d\n", i);
	
        g_hThreadArray[i] = CreateThread(
            NULL,				// default security attributes
            0,					// use default stack size
            processStressThreads,		// thread function
            0,					// argument to thread function
            0,		                        // use default creation flags
            &dwThreadId);		        // returns the thread identifier

        if (g_hThreadArray[i] == NULL)
        {
            LogError(L"CreateThread failed.", __WFILE__, __LINE__);
            LogLastError();
            return bReturn;
        }
    }
    bReturn = TRUE;
    return bReturn;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DWORD WINAPI processStressThreads(int tdata)
{
	printf("ProcessingStressThreads\n");
	UINT driverSelect;
	UINT operationSelect;

	for(int cycle=1; cycle<=MAX_CYCLES; ++cycle)
	{
		driverSelect=rand()%5 + 1;
		operationSelect=rand()%2 + 1;

		printf("Thread data=%d, driverSelect=%d, operationSelect=%d\n", tdata, driverSelect, operationSelect); 
	}

	return 1;
}


I just call createStressThreads from the main function.

THis is the error that I get:
 
error C2664: 'CreateThread' : cannot convert parameter 3 from 'DWORD (__cdecl *)(int)' to 'LPTHREAD_START_ROUTINE'


What's wrong with the code above?

Thanks.
The processStressThreads function should take a void pointer as an argument. See here for more info:

http://msdn.microsoft.com/en-us/library/ms682453%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms686736%28v=VS.85%29.aspx
It worked. Thanks!
Don't call CreateThread, it doesn't initialise the C runtime for the thread. Use _createthreadex instead.
kbw, Mine is a simple program and am not using any C runtime libraries for the thread. Created threads call a function to load/unload drivers and that's about it. I am thinking CreateThread should work there.
n4nature wrote:
am not using any C runtime libraries for the thread

Hmmm... If you don't use any of the functions listed here, I guess you're ok...

http://www.filtermeister.com/wiki/index.php?page=C+Runtime+Functions

More info on the problem:

Threads that are created and terminated with the CreateThread() and ExitThread() Win32 API functions do not have memory that is allocated by the CRT for static data and static buffers cleaned up when the thread terminates. Some examples of this type of memory are static data for errno and _doserrno and the static buffers used by functions such as asctime(), ctime(), localtime(), gmtime(), and mktime(). Using CreateThread() in a program that uses the CRT (for example, links with LIBCMT.LIB) may cause a memory leak of about 70-80 bytes each time a thread is terminated.

Found here -> http://support.microsoft.com/kb/104641

And here are some things that printf might do and cause a problem:

http://bytes.com/topic/c/answers/811441-query-about-printf
Mine is a simple program and am not using any C runtime libraries for the thread.

You do call rand() in your example.
Ok, Thanks kbw and m4ster r0shi, that helps a lot!
Topic archived. No new replies allowed.