Click to See Complete Forum and Search --> : C++ Threads


starfleetrp
July 14th, 2005, 06:07 PM
Hi,
I am a complete c++ noob,
I have a question about how to create multiple threads, if you could show me some code, it would be a great help.

For example I would like to do

int main()
{
thread1();
thread2();
}

void thread1()
{
for(int i=1;i==5;i++)
{
std::cout i;
}
}

void thread2()
{
std::cout "hi";
}


so it would output something like 1hi2345 instead of 12345hi, so how would you do this... I know it has to be multithreaded but how?

golanshahar
July 14th, 2005, 06:21 PM
if you want "1hi2345" output simply use printf("1hi2345"); ;)
kidding.
ok you want to do it with thread?
take a look at this FAQ (http://www.codeguru.com/forum/showthread.php?t=312452&highlight=thread).

just you to know no one gurantee about the output, most probably the result will be hi12345 or 12345hi and not like what you want!


Cheers

Radius
July 14th, 2005, 06:24 PM
First of all, look up CreateThread(), that's how you get them going. Here's a little example based on what you have:

int main()
{
HANDLE hThread1;
HANDLE hThread2;

hThread1 = CreateThread(NULL, 0, thread1, NULL, 0, NULL);
hThread2 = CreateThread(NULL, 0, thread2, NULL, 0, NULL);

Sleep(500);

CloseHandle(hThread1);
CloseHandle(hThread2);
}

DWORD thread1(LPVOID)
{
for(int i=1;i==5;i++)
{
std::cout i;
}

return 0;
}

DWORD thread2(LPVOID)
{
std::cout "hi";

return 0;
}

Ok, the Sleep(500) thingy is just to let your threads finish before closing the handles to those threads. in reality, you would probably do something a lot more complex here and allow the threads to keep running. Still, this is a good way to see what's happening.

I haven't tried to compile this, but I think I got everything in there you need. Hope this helps!

Andreas Masur
July 15th, 2005, 02:30 AM
[ Redirected thread ]

Andreas Masur
July 15th, 2005, 02:30 AM
How to create a worker thread? (http://www.codeguru.com/forum/showthread.php?t=312452)
How to end a thread? (http://www.codeguru.com/forum/showthread.php?t=305166)
How to use member functions as thread functions? (http://www.codeguru.com/forum/showthread.php?t=312453)
How to access UI elements from a thread in MFC? (http://www.codeguru.com/forum/showthread.php?t=312454)

NMTop40
July 18th, 2005, 11:13 AM
From which forum was this redirected? Where did the poster say he was programming on the Windows platform?

There is no standard way to create a thread in C++ (or C). However, boost libraries have created thread classes which wrap the threading functionality on various platforms, particularly pthreads (UNIX and Linux) and Windows threads.

Normally you will want your main function to "join" or "wait for" your threads before exiting. (The term "join" means that the two threads, i.e. the main thread and the one you created, merge into one thread again).

Andreas Masur
July 18th, 2005, 06:00 PM
From which forum was this redirected? Where did the poster say he was programming on the Windows platform?
It was being redirected from the "Visual C++ Programming" forum...

wildfrog
July 18th, 2005, 06:46 PM
Ok, the Sleep(500) thingy is just to let your threads finish before closing the handles to those threads. Just a tiny comment: Closing the thread handles won't affect the threads, they'll continue running until they're done (or terminated).

- petter