Archived

This topic is now archived and is closed to further replies.

LastResort

Win32 threads

Recommended Posts

Hello all! Im having a litle problem running threads under win9x. If i compile the source code under NT/XP the threads work like they should, but if i compile under win98 it dosent work.. My guess is that im missing something but i dont know.. sometime ago, someone told be to put a #define and it should work.. but it didnt Anyone has any idea of what might be my problem? Thanks in advance

Share this post


Link to post
Share on other sites
You''re using some kind of function that isn''t supported under win9x

And we can''t help you with that unless you post some source code.

Share this post


Link to post
Share on other sites
Ok, the code:


  
#define _WIN32_WINNT 0x0500 // the #define that i mentioned


//includes


//prototypes

DWORD WINAPI ActualizaStatus(LPVOID param);

int WINAPI WinMain(...)
{
// create window and initialize stuff


CreateThread(NULL,0,ActualizaStatus,(HWND)hWnd,0,NULL);

// msgs handler

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam)
{
//nothing important for the thread

}


// the thread itself

DWORD WINAPI ActualizaStatus(LPVOID param)
{
HANDLE hTimer;
SYSTEMTIME systime;
FILETIME filetime;
LARGE_INTEGER li;

GetSystemTime(&systime);
SystemTimeToFileTime(&systime,&filetime);
li.LowPart=filetime.dwLowDateTime;
li.HighPart=filetime.dwHighDateTime;
li.QuadPart+=0;
hTimer=CreateWaitableTimer(NULL,0,"StatusTimer");
SetWaitableTimer(hTimer,&li,1000,NULL,NULL,0);
while(1)
{
WaitForSingleObject(hTimer,INFINITE);
StatusReport((HWND)param); // unimportant function

}
}


Yes, this code works.. at least on NT/XP.. on win98 its as if the code wasnt there.. (bunch of NOP''s kiding)

This is the third time i try to answer..
1º time page error
2º computer crash .. weeee

(btw, sorry 4 my bad english)

Share this post


Link to post
Share on other sites
For starters try _beginthreadex instead of CreateThread. You'll probably need to include process.h


How does the thread exit??

[edited by - Solo on October 21, 2002 1:54:32 PM]

Share this post


Link to post
Share on other sites
It ends when the app ends..

btw, i get "warning C4013: '_beginthreadex' undefined; assuming extern returning int"

and i have, #include <process.h>
and also
#pragma comment(lib, "LIBCMT.LIB")
#pragma comment(lib, "MSVCRT.LIB")

weird..
even the example doesnt work :| (in the MSDN)

[edited by - lastresort on October 21, 2002 2:15:28 PM]

Share this post


Link to post
Share on other sites
the example is :

  
#include <windows.h>
#include <process.h> /* _beginthread, _endthread */
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>

void Bounce( void *ch );
void CheckKey( void *dummy );

/* GetRandom returns a random integer between min and max. */
#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))

BOOL repeat = TRUE; /* Global repeat flag and video variable */
HANDLE hStdOut; /* Handle for console window */
CONSOLE_SCREEN_BUFFER_INFO csbi; /* Console information structure */

void main()
{
CHAR ch = ''A'';

hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );

/* Get display screen''s text row and column information. */
GetConsoleScreenBufferInfo( hStdOut, &csbi );

/* Launch CheckKey thread to check for terminating keystroke. */
_beginthread( CheckKey, 0, NULL );

/* Loop until CheckKey terminates program. */
while( repeat )
{
/* On first loops, launch character threads. */
_beginthread( Bounce, 0, (void *) (ch++) );

/* Wait one second between loops. */
Sleep( 1000L );
}
}

/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
void CheckKey( void *dummy )
{
_getch();
repeat = 0; /* _endthread implied */

}

/* Bounce - Thread to create and and control a colored letter that moves
* around on the screen.
*
* Params: ch - the letter to be moved
*/
void Bounce( void *ch )
{
/* Generate letter and color attribute from thread argument. */
char blankcell = 0x20;
char blockcell = (char) ch;
BOOL first = TRUE;
COORD oldcoord, newcoord;
DWORD result;


/* Seed random number generator and get initial location. */
srand( _threadid );
newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );
newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );
while( repeat )
{
/* Pause between loops. */
Sleep( 100L );

/* Blank out our old position on the screen, and draw new letter. */
if( first )
first = FALSE;
else
WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );
WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );

/* Increment the coordinate for next placement of the block. */
oldcoord.X = newcoord.X;
oldcoord.Y = newcoord.Y;
newcoord.X += GetRandom( -1, 1 );
newcoord.Y += GetRandom( -1, 1 );

/* Correct placement (and beep) if about to go off the screen. */
if( newcoord.X < 0 )
newcoord.X = 1;
else if( newcoord.X == csbi.dwSize.X )
newcoord.X = csbi.dwSize.X - 2;
else if( newcoord.Y < 0 )
newcoord.Y = 1;
else if( newcoord.Y == csbi.dwSize.Y )
newcoord.Y = csbi.dwSize.Y - 2;

/* If not at a screen border, continue, otherwise beep. */
else
continue;
Beep( ((char) ch - ''A'') * 100, 175 );
}
/* _endthread given to terminate */

_endthread();
}



and the errors/warnings :

  
Compiling...
teste.c
C:\test\test.c(27) : warning C4013: ''_beginthread'' undefined; assuming extern returning int
C:\test\test.c(64) : error C2065: ''_threadid'' : undeclared identifier
C:\test\test.c(101) : warning C4013: ''_endthread'' undefined; assuming extern returning int
Error executing cl.exe.

Share this post


Link to post
Share on other sites
quote:
Original post by LastResort
It ends when the app ends..



The process doesn''t end until the threads end and this:


  
...
while(1)
{
WaitForSingleObject(hTimer,INFINITE);
StatusReport((HWND)param); // unimportant function

}


doesn''t end.

It also looks like your not linking with the multithreaded runtime libraries. This may have been the problem all along (however _beginthreadex should always be used over CreateThread).

Share this post


Link to post
Share on other sites
I can always change while(1) by while(RUNNING) or something.. that was just an example.. I have other threads that do their job and end before the process ends. But, when the app ends, so does the process, wich means, all current threads end.. even if they are on a while(1). Correct me if im wrong

quote:

It also looks like your not linking with the multithreaded runtime libraries. This may have been the problem all along (however _beginthreadex should always be used over CreateThread).


What multithreaded runtime libraries?

Share this post


Link to post
Share on other sites
Using #pragma to set the C runtime library won''t work. You have to go into project settings, and change it from there. It''s in the C/C++ language / code generation bit.

Sorry I can''t be more precise - I don''t have MSVC on this machine.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
To use multithreaded libraries in MSVC:

1) Click "Project" and then "Settings".
2) Click the "C/C++" tab.
3) From the Category drop down choose "Code Generation".
4) Choose an appropriate library for your project under "Use Run-time Library".


quote:

I can always change while(1) by while(RUNNING) or something.. that was just an example.. I have other threads that do their job and end before the process ends. But, when the app ends, so does the process, wich means, all current threads end.. even if they are on a while(1). Correct me if im wrong



Actually you''re right and I''m wrong. Somehow I got the idea that a running thread will keep the process alive (probably because I usually wait for my worker threads to end before I allow the primary thread to exit). MSDN says the opposite and so did the test I ran. Anyway, you should still allow the thread to exit in a controlled fashion. If another thread (like the primary thread) releases resources used by StatusReport() during shutdown (like HWND''s of windows that have been closed) your runnaway thread could generate errors.

Share this post


Link to post
Share on other sites
Change your filename from .c to .cpp.
MSVC was moaning at me today for that.

HTH, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
quote:
Original post by Solo
Actually you''re right and I''m wrong. Somehow I got the idea that a running thread will keep the process alive (probably because I usually wait for my worker threads to end before I allow the primary thread to exit). MSDN says the opposite and so did the test I ran. Anyway, you should still allow the thread to exit in a controlled fashion. If another thread (like the primary thread) releases resources used by StatusReport() during shutdown (like HWND''s of windows that have been closed) your runnaway thread could generate errors.


Your very right.. we should always avoid nasty errors

Anyway, as soon as i get home ill try that (the multithreaded libraries).

quote:
Original post by DigitalDelusion
Ok, I guess this actually falls under the RT(F)M category:
The last parameter of CreateThread can''t be NULL under Win9x.



Ill try that also

Evil Bill, ill try that one more time hehehe

Thaks all for the help

Share this post


Link to post
Share on other sites
Well, DigitalDelusion you are right, it cant be NULL..
It works, finaly, with or without the run time libs.
Thanks to you all


  
...
LONG thread_id;
CreateThread(NULL,0,ActualizaStatus,(HWND)hWnd,0,&thread_id);
...


Bye

Share this post


Link to post
Share on other sites