|
-
October 15th, 2005, 03:49 AM
#1
a question about multiplethread
now i use several threads to wite a serial port ,i want only one thread to write the port at the same time ,I don't know how to do it . i hope some one help me soon
-
October 15th, 2005, 04:23 AM
#2
Re: a question about multiplethread
Take a look at Mutex's and Critical Sections.
Jim
ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII
"The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.
"Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.
-
October 15th, 2005, 05:32 AM
#3
Re: a question about multiplethread
[ redirected ]
Regards,
Siddhartha
-
October 15th, 2005, 05:40 AM
#4
Re: a question about multiplethread
The easiest approach to restricting the number of threads that execute a piece of code to one is via Critical Sections.
One works with Critical Sections using Win APIs -
So, in your case, the code would be something like this -
Creating and Initializing:
Code:
// A global object, or a globally accessible object (like static member)
CRITICAL_SECTION myCS;
// In the main thread
::InitializeCriticalSection (&myCS);
Using Critical Sections:
Code:
// Inside the worker thread
// At the point in code that needs to be protected
// And needs to be entered one thread at a time
::EnterCriticalSection (&myCS);
// The lines of code that need to be protected
::LeaveCriticalSection (&myCS);
Finally, Deleting:
Code:
// Again in the main thread, after the worker threads
// Have ended and the CS is not needed
::DeleteCriticalSection (&myCS);
Hope, you now have a clear idea of the way to proceed.
Last edited by Siddhartha; October 15th, 2005 at 05:43 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|