Solved

Synchronisation of Threads in VC++

Posted on 1998-06-10
8
240 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 50 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
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!

 
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

Independent Software Vendors: 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

734 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