Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

THREADS

Status
Not open for further replies.

preeth24

Programmer
May 19, 2003
2
IN
What might be the problem in this code

void ThreadFunc2( )
{
char szMsg[80];
wsprintf( szMsg, "Parameter = %s.", "Inside thread func2");
MessageBox( NULL, szMsg, "ThreadFunc2", MB_OK );
return 0;
}

void ThreadFunc1( )
{
char szMsg[80];
wsprintf( szMsg, "Parameter = %s.", "Inside thread func1");
MessageBox( NULL, szMsg, "ThreadFunc1", MB_OK );

DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;

hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE)ThreadFunc2, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier

// Check the return value for success.

if (hThread == NULL)
{
wsprintf( szMsg, "CreateThread failed." );
MessageBox( NULL, szMsg, "main", MB_OK );
}
else
{
WaitForSingleObject( hThread,INFINITE);
CloseHandle( hThread );
}

return 0;
}

void main( void )
{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;
char szMsg[80];

hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE)ThreadFunc1, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier

// Check the return value for success.

if (hThread == NULL)
{
wsprintf( szMsg, "CreateThread failed." );
MessageBox( NULL, szMsg, "main", MB_OK );
}
else
{
_getch();
CloseHandle( hThread );
}
}


HERE THREADs are CREATED BUT THE FUNCTION IS NOT GETTING EXECUTED.....

 
In order to use C Runtime Library functions (such as your wsprintf in ThreadFunc2) you must create threads with _beginthread or _beginthreadex (and it follows that you must use _endthread instead of ExitThread).

The _beginthreadex function has the same parameter form as CreateThread.

I REALLY hope that helps.
Will
 
Thanx...a lot...

Though I've come out with a solution as to why the Thread was not executing..this seems to be an interesting point.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top