Of course. I believe the function originally used was beginthread. But since console proggies these days are merely Windows programs in disguise, you'd be better off using CreateThread().
Try this:
Code:
/*
Keep these in mind...
BOOL SuspendThread(HANDLE thread);
BOOL ResumeThread(HANDLE thread);
BOOL SetThreadPriority(HANDLE thread, int priority);
int GetThreadPriority(HANDLE thread);
THREAD_PRIORITY_IDLE
THREAD_PRIORITY_LOWEST
THREAD_PRIORITY_BELOW_NORMAL
THREAD_PRIORITY_NORMAL
THREAD_PRIORITY_ABOVE_NORMAL
THREAD_PRIORITY_HIGHEST
THREAD_PRIORITY_TIME_CRITICAL
*/
#define THREAD HANDLE
#define THREADFUNC DWORD WINAPI
THREAD
NewThread( DWORD func(LPVOID), LPVOID params = NULL, DWORD *id = NULL)
{
static DWORD fakeIt = 0;
if(id == NULL)
{
fakeIt++;
id = &fakeIt;
}
return CreateThread(NULL, 0, func, (LPVOID)params, 0, id);
}
THREAD
NewThreadWaiting( DWORD func(LPVOID), LPVOID params, DWORD *id = NULL)
{
static DWORD fakeIt = 0;
if(id == NULL)
{
fakeIt++;
id = &fakeIt;
}
return CreateThread(NULL, 0, func, params, CREATE_SUSPENDED, id);
}
bool ThreadIsActive(THREAD t)
{
DWORD status = 0;
GetExitCodeThread(t, &status);
if(status == STILL_ACTIVE)
return true;
CloseHandle(t);
return false;
}
About threads:
When you create a thread there will be a couple of things to do/remember:
1) Define the function to pass to "NewThread()"
2) A thread will not hold a console program "open", thus at the end of the program, be sure to wait for the thread before exiting.
3) There is no guarantee when a thread begins, exits, and how long it gets to execute each time slice.
4) When in doubt, USE A critical section, mutex, or semaphore to protect shared data.
Here's a sample app:
Code:
THREADFUNC ThreadFunc( void *param ) //...the parameter is mandatory, even if unused...
{
printf("\n I live!!!");
printf("\n Message: %s", (param == NULL) ? "none" : (char*)param);
Sleep(5000);
printf("\n I'm dying!!!");
}
int main()
{
char msg[] = "Thread Two Gets A Message.";
THREAD thread = NewThread( ThreadFunc );
THREAD alert = NewThread( ThreadFunc, (void*)msg);
while( ThreadIsActive(thread) ); //...wait around...
while( ThreadIsActive(alert) ); //...wait around...
return 0;
}
Have fun!