Solved

Semaphores in Windows

Posted on 2003-11-04
7
1,874 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
Comment Utility
I think it'll be better to use CriticalSections in such a case.
0
 
LVL 1

Expert Comment

by:pohheng
Comment Utility
0
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 20 total points
Comment Utility
#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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now