How to use thread in c++?

Started by
9 comments, last by yungivan 21 years, 7 months ago
Hi, I''m currently work on a vc++ TCP client/server program with winsock, how can I apply threading in vc++? Is there any different in coding between applying thread in console and in MS Windows?
Advertisement
You will be able to use multithreading in a console applications without any problems. I did it before.

I recommend you use _beginthreadex() to create your threads since you will probably want to use C I/O when using winsock.

Looking for a serious game project?
www.xgameproject.com
quote:Original post by Max_Payne
You will be able to use multithreading in a console applications without any problems. I did it before.

I recommend you use _beginthreadex() to create your threads since you will probably want to use C I/O when using winsock.


Multithreading is platform-dependent. _beginthread() and _beginthreadex() are not ANSI compatible. If you''re throwing compatibility out the window anyways, why not just use CreateThread() (other than not #include''ing windows.h)?

"If people are good only because they fear punishment and hope for reward, then we are a sorry lot indeed." - Albert Einstein
quote:Original post by core
Multithreading is platform-dependent.

Maybe, but the Boost threads library goes some way to presenting a standard interface. I recommend a look. (See my signature for a link to the Boost library.)



[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
_beginthreadex() not ANSI compatible? What the hell are you saying, CreateThread() creates memory leaks when using the C run-times, which you most likely will be using when programming with winsock...... Unless you prefer the win32 replacement functions to the C run-times....

Dont believe me about CreateThread() creating memory leaks? check msdn.



[edited by - Max_Payne on September 11, 2002 6:59:10 PM]

Looking for a serious game project?
www.xgameproject.com
he''s saying _beginthread() is not a part of any ANSI standard library (such as stdio, stdlib, any of the STL etc.)
The closest thing to a threading standard you can get is the POSIX thread (pthreads) interface.

Or the Boost thread library, which uses windows threads or pthreads depending on what it finds on your machine when you install it.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thank all of you.
I have tried to use _beginthreadex(), but there are errors when compiling it, I''m wondering if this is a library link problem, then I tried to use CreateThread(), and it works. What do you think the possible reason that causes the problem? Which function is better?
Try this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt__beginthread.2c_._beginthreadex.asp

-SirKnight
_beginthreadex() is better because it will avoid you the memory leak problems.

Please remember that to use _beginthreadex() you have to include a new header (process.h i believe), you also have to change your C run-times to multithreaded (see project settings, c++, code generation), instead of single threaded... And then, you have to rebuild all, because the compiler won't do it for you (well, it wont in vc++ 6).

I havent ever used thread libraries, but changing a thread call and a function prototype if porting to linux should not be so complicated, just some ifdefs.

[edited by - Max_Payne on September 12, 2002 12:56:34 PM]

Looking for a serious game project?
www.xgameproject.com

This topic is closed to new replies.

Advertisement