?
Solved

Synchronisation of Threads in VC++

Posted on 1998-06-10
8
Medium Priority
?
247 Views
Last Modified: 2008-02-01
I am creating threads using API calls. So I cannot use the seamophores class using MFC.

So is there any way of synchronising the threads. I have few global variables which are being accessed by two or more threads. So how do I synchronise them.

Thanks in advance

nikhilh

0
Comment
Question by:nikhilh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1165580
You can use the MFC classes CSemaphore, CSingleLock or CMultiLock to synchronize your threads no matter whether you are using MFC to create the threads. These classes are the wrapper for Semaphore object and the waiting functions. You can also directly use them (CreateSemaphore, WaitForSingleObject, etc). Visaul C++ comes with several multithread samples (MFC and non-MFC).
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165581
That is MFC, though.

You can use CreateMutex() to create mutexes, CreateSemaphore() to create semaphores, and CreateEvent(0 to create events.  These are all regulat API calls.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165582
Oh I see.  chensu interpretted this as an MFC program that doesn't use MFC to create threads.  I interpretted it as a non-MFC program.  Which is right?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:alexo
ID: 1165583
Critical sections are much more efficient than mutexes and have similar functionality.

APIs:
  InitializeCriticalSection()
  EnterCriticalSection()
  LeaveCriticalSection()
  DeleteCriticalSection()

MFC object: CCriticalSection
  CCriticalSection() - Constructs a CCriticalSection object.
  Unlock() - Releases the CCriticalSection object.
  Lock() - Use to gain access to the CCriticalSection object.

Tell me it it helps.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165584
Do you know for a fact that they are more efficient?  I always assumed they were implimented using mutexes.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1165585
Yes, I do know that for a fact.

A CS only works between threads of a single process so no kernel call (ring 3 --> ring 0 --> ring 3 transition) needs to be made if the CS is free.

I once did an emphirical timing and, if I remember correctly, CS calls were about 20x faster than mutex calls with no blocking (timing with blocking is all but meaningless).

References:
http://www.dejanews.com/dnquery.xp?search=thread&recnum=%3c337b4b70.83813086@neptune%3e%231/1
http://www.dejanews.com/dnquery.xp?search=thread&recnum=%3c34983a37.166416534@news.netvision.net.il%3e%231/1
0
 
LVL 2

Expert Comment

by:abesoft
ID: 1165586
alexo:
Any data on Events, Semaphores, etc?  I assume that these are in the same category as CS, wrt speed.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1165587
>> I assume that these are in the same category as CS, wrt speed.
Same order of magnitude as *mutexes*.  Don't remember the exact details but the results were pretty close.  CSs are much faster because you avoid the kernell call that you must take when working with "real" sync. objects (those you can pass to WaitFor...() APIs).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question