?
Solved

Semaphores in Windows

Posted on 2003-11-04
7
Medium Priority
?
1,899 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
[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
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

718 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