Solved

Synchronisation of Threads in VC++

Posted on 1998-06-10
8
239 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

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 100
IdTCPClient1->Disconnect(); not working 3 86
Unable to start eclipse ? 17 163
c++  placing data into a form and an editbox 5 23
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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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