Solved

Semaphores in Windows

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now