By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

multi threading in C

P: 12
Pls. give some sample code for understanding multithreading in C.
Sep 28 '06 #1
Share this Question
Share on Google+
4 Replies


Banfa
Expert Mod 5K+
P: 8,916
There is no multithreading in C, there may be some provided by your platform though
Sep 28 '06 #2

P: 59
you can use winAPI to do that if you are writing a simple console application (IE CreateThread())
However, it is not recommended to use API commands directly because they cause instability.
Sep 28 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
However, it is not recommended to use API commands directly because they cause instability.
Where on earth did you hear that and if you don't use them directly how should you use them? Through the bloat code that is the MFC?

I have been successfully using the Windows API directly for 15 years.
Sep 28 '06 #4

P: 59
I haven't done multithreading in C/C++, but the direct use of WINAPI thread functions in VB or VB.NET create some major instabilites in the program. Like, you cannot call the same function more than once or you'll get an exception. Sometimes the program would just hang executing a threaded function. So you end up using some components to do threading for you. I don't know if it's the same in C/C++.

looking at the API documentation you should be able to execute a thread using this code (C++)
Expand|Select|Wrap|Line Numbers
  1.     HANDLE mHandle;
  2.     DWORD data;
  3.     mHandle = CreateThread( NULL, 0, threaded_func, &data, 0, NULL);
  4.  
  5.         // your threaded function would look something like this
  6.         DWORD WINAPI threaded_func (LPVOID myParam) {
  7.         // do something
  8.         }
  9.  
Note that you'll have to use WinAPI to communicate with the program from the threaded function (instead of cout you'd use WriteConsole() and etc)
Sep 29 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.