Solved

Synchronisation of Threads in VC++

Posted on 1998-06-10
8
231 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

773 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