Archived

This topic is now archived and is closed to further replies.

How to use thread in c++?

This topic is 5572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
_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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
he''s saying _beginthread() is not a part of any ANSI standard library (such as stdio, stdlib, any of the STL etc.)

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
_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]

Share this post


Link to post
Share on other sites
quote:
Original post by core
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)?


As already mentioned, you shouldn''t call CreateThread when using C/C++ runtime libraries. The __beginthreadex() function prepares the C/C++ runtime libraries for multithreading using TLS.

------

In order to successfully compile a source file with calls to __beginthreadex you must include AND make sure you are using multithreaded libraries (static or DLL.) Check your Project settings under the C++ tab, and under Code Generation.

Share this post


Link to post
Share on other sites