Solved

Semaphores in Windows

Posted on 2003-11-04
7
1,894 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 20 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

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.

751 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