Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2007

Tip: Looking for answers? Try searching our database.

Why the thread can not be created?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jack - 27 Jan 2007 05:38 GMT
Hi,

In the code below:

#include <process.h>

unsigned __stdcall ReadThread( void *arg )
{
    while( 1 )
    {
        cout << "RUN" << endl;
    }
    _endthreadex( 0 );
    return 0;
}

int main( int argc, char **argv )
{

       .......

       HANDLE hThread;
    unsigned threadID;
    hThread = (HANDLE)_beginthreadex( NULL, 0, &ReadThread, NULL, 0,
&threadID ); //LINE1
    if (hThread == 0) {
            cout << "Failure in _beginthreadex(), errno: " << endl;
       }

}

Why at LINE1, hThread is always 0, i.e., the thread is not created?

I use Visual Studio .NET 2003.

Thanks.

Jack
Doug Harrison [MVP] - 27 Jan 2007 06:05 GMT
>Hi,
>
[quoted text clipped - 34 lines]
>
>Jack

When I:

1. Add the following to the top:

#include <windows.h>
#include <iostream>
using namespace std;

2. Delete the "......." line.

3. Compile with cl -EHsc -MD a.cpp.

The output is "RUN" in VC 2003. BTW, there's rarely any reason to use
_endthreadex. Simply returning from the thread function accomplishes the
same thing. Also, you shouldn't allow your secondary threads to continue
running while the main thread is shutting down the process and destroying
the environment in which the secondary threads run. Instead, the main
thread should join with all the secondary threads prior to terminating. You
would typically accomplish this by designing some mechanism to notify the
secondary threads to terminate and then use WaitForSingleObject or its
Multiple variants to join with them.

Signature

Doug Harrison
Visual C++ MVP

Jack - 27 Jan 2007 06:44 GMT
> >Hi,
>
[quoted text clipped - 44 lines]
>
> 3. Compile with cl -EHsc -MD a.cpp.

Thanks a lot. I am learning Win32 thread. I compiled my code from the
MENU of the window, i.e., clicking the "Build" button. How should I set
up "Project Properties" to get the options of the above command line?

By the way, if I also want to run the above command line, which
directory should I enter?

Jack

> The output is "RUN" in VC 2003. BTW, there's rarely any reason to use
> _endthreadex. Simply returning from the thread function accomplishes the
[quoted text clipped - 9 lines]
> Doug Harrison
> Visual C++ MVP

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.