Link to home
Start Free TrialLog in
Avatar of SR_SQL
SR_SQL

asked on

Semaphores!!!!

Can anyone help me to invoke or link those threads or make threads wait on the semaphore created?

For example, I create 10 threads, I need to make these threads wait on a semaphore and whenever a semaphore signals, one thread should be pciked.

In the example 10 threads are created. How will I use semaphores to signal anyone of these threasd?


#include <iostream.h>
#include <windows.h>

DWORD WINAPI TT1();
DWORD threadid;

void main()
{

HANDLE hMyThread[10];

DWORD MyThreadID[10];

      for(int i=0; i<10;i++)
      {
                    hMyThread[i] = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)TT1,
                                                         NULL, 0, &MyThreadID[i]);
            
            threadid = *(&MyThreadID[i]);

            cout<< "threadid" << threadid <<endl;
            
            WaitForSingleObject(hMyThread, INFINITE);

            cout<< "threadid" << *(&MyThreadID[i]) <<endl;
            
      }           
       
}

DWORD WINAPI TT1()
{
      
      cout << "In TT1 - Thread Created" <<endl;
      return 0;

}


Any help appreciated.
Please let me know
thanks
Avatar of Ajar
Ajar

If  your requierement is to achieve synchronization between many threads running in a
same process only you can use  criticalSection apis   instead of semaphores which I think are
system wide locks .

The main funcitons are  :

#Create  Critical section
#Enter  Critical section
#leave  Critical section
#Delete  Critical section


have a look at these apis

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/synchronization_functions.asp
ASKER CERTIFIED SOLUTION
Avatar of pohheng
pohheng

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial