Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Semaphores in Windows

Posted on 2003-11-04
7
Medium Priority
?
1,902 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:sudha_sql
  • 2
7 Comments
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9686376
I think it'll be better to use CriticalSections in such a case.
0
 
LVL 1

Expert Comment

by:pohheng
ID: 9692001
0
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 80 total points
ID: 9693343
#include <stdio.h>
#include <iostream.h>
#include <windows.h>

DWORD WINAPI TT1(LPVOID lpParameter);
DWORD threadid;

HANDLE g_hEvent;

void main()
{
  HANDLE hMyThread[10];
  DWORD MyThreadID[10];

  g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

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

  while (1)
  {
    char ch = getchar();
    if (ch == 'e')
    {
      break;
    }
    if (ch == 's')
    {
      SetEvent(g_hEvent);
    }
  }

  CloseHandle(g_hEvent);
}

DWORD WINAPI TT1(LPVOID lpParameter)
{
  cout << "In TT1 (" << (int)lpParameter << ") - Thread Created" << endl;
  WaitForSingleObject(g_hEvent, INFINITE);
  cout << "In TT1 (" << (int)lpParameter << ") - Event Set" << endl;
  return 0;
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

926 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