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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Threads

Status
Not open for further replies.

JJ1

Programmer
Apr 19, 2001
104
GB
Hi All,

I'm trying to learn concurrent programming and am a novice to C/C++ - Could anyone help please?

I hope to create a thread using the 'CreateThread' API call and print just 2 simple messages. Unfortunately, the only message that appears is 'Hello from the main function' - nothing from 'MyThread1'. Could anyone tell me what is wrong please?

Also, I need to use semphores later on in my course. How are these declared?


#include <windows.h>
#include <stdio.h>

//prototypes
long WINAPI MyThread1(long lParam);

int main(void)
{
HANDLE hThread;
DWORD dwThreadID;

hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThread1, NULL, 0, &dwThreadId);
printf( &quot;Hello from the main function&quot; );
return 0;
}

long WINAPI MyThread1(long lParam)
{
printf(&quot;Thread 1 active&quot;);
return 0L;
}

Thanks for your time and help,

James.
 
James,

I'm a really newbie to threading as well, I'm about to encounter my first project using them! So I've been doing a bit of reasearch and thing I can actually see what is wrong with your code.

The problem appears to be that your main thread is terminating before the new thread is getting started!

If you put a getch() or Sleep(x) in after you've created the new thread, you will see that it is working

Hope that helps
-Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top