473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why the thread can not be created?

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

Jan 27 '07 #1
2 1178
On 26 Jan 2007 21:38:54 -0800, "Jack" <ju******@gmail.comwrote:
>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
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.

--
Doug Harrison
Visual C++ MVP
Jan 27 '07 #2


On Jan 26, 10:05 pm, "Doug Harrison [MVP]" <d...@mvps.orgwrote:
On 26 Jan 2007 21:38:54 -0800, "Jack" <junw2...@gmail.comwrote:
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.
JackWhen 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.
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
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.

--
Doug Harrison
Visual C++ MVP
Jan 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Xarky | last post by:
Hi, I created a thread, that is creating an array of labels and is writing them to my windows form, exatly on a tab page. But it is giving me the following error. An unhandled exception of...
11
by: PJ | last post by:
I'd like to create a subsystem in my asp.net application that is responsible for emails that need to be send out based upon certain events so that the main request/response threads aren't...
9
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException...
6
by: Brian Gideon | last post by:
How have you handled the finalization of thread-specific unmanaged resources? My question pertains specifically to using the DDEML which is a thread-specific API. In other words, every call to...
7
by: MariusI | last post by:
Are objects implicitly stored in the TLS of the currently running thread? When creating multithreaded applications i get errors when accessing data from a different thread than the thread used to...
3
by: Gary Wessle | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; class waiter { public: waiter(); void waiting(); void preform();
12
by: Tomaz Koritnik | last post by:
Is it possible to asynchronously call a method from worker thread so that this method would execute in main-thread? I'm doing some work in worker thread and would like to report progress to main...
8
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I have an application with several BackgroundWorker threads. I hoped I'd be able to just type backgroundworker1.Name = "bw1"; but I don't see a name property. Any thoughts on how to name a...
14
by: shark | last post by:
Hi, Does Form.ShowDialog() start new thread ? If yes how is solved cross-thread operations? Thx
3
by: =?Utf-8?B?SlQ=?= | last post by:
If I call Thread.Start am I guaranteed that thread will be running before the call from Thread.Start returns? i.e. //Doing Something on main thread Thread.Start(NewThreadWork);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.