#Include #Include Using Namespace STD Handle Hthreada, Hthreadb Handle Hsemaphore

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

#include <iostream>

#include <Windows.h>
using namespace std;
HANDLE hthreadA, hthreadB;
HANDLE hsemaphore;
//semaphore counting k liye use hta hai.. jysy k
hmain aik particular amount of threads chahiyen k
itni threads hi enter hon mtlb hm ny limit lgani hai
tou hm semaphore sy protect kraingy usy.. jitni
threads allow krni hain hmain us number sy
semaphore ko initialize krwa daingy
DWORD threadid, threadid2;
//a dword is an unsigned, 32-bit unit of data. It can
contain an integer value in the range 0 through
4,294,967,295.
int a,b, c;
//LPVOID is simply a Windows API typedef for void*
pointer--
DWORD WINAPI MyFunA(LPVOID lpParam)
//WINAPI is a macro that expands to __stdcall
which means that the callee cleans the stack.
{
cout<<"Enter value of a ";
cin>>a;
cout<<"Enter value of b ";
endl --> next line
cin ---> input
cout ---> output/print
cin>>b;
cout<<endl;
ReleaseSemaphore(hsemaphore, 1, 0);
When a thread completes the task, it uses the
ReleaseSemaphore function to increment the
semaphore's count, thus enabling another waiting
thread to perform the task.
}
DWORD WINAPI MyFunB(LPVOID lpParam)
{
WaitForSingleObject(hsemaphore, 'INFINITE');
//Before a thread attempts to perform the task, it uses
the WaitForSingleObject function to determine whether
the semaphore's current count permits it to do so. The
wait function's time-out parameter is set to zero, so the
function returns immediately if the semaphore is in the
nonsignaled state. WaitForSingleObject decrements the
semaphore's count by one.

c=a+b;
cout<<"sum of a and b is "<<c<<endl;
}
int main()
{
hthreadA = CreateThread(NULL, 0, MyFunA,
NULL, 0, &threadid);
hthreadB = CreateThread(NULL, 0, MyFunB,
NULL, 0, &threadid2);
hsemaphore = CreateSemaphoreW(NULL, 0, 1,
NULL);

//Waits until the specified object is in the signaled


state or the time-out interval elapses.
WaitForSingleObject(hthreadA, 'INFINITE');
WaitForSingleObject(hthreadB, 'INFINITE');
CloseHandle(hsemaphore);
CloseHandle(hthreadA);
CloseHandle(hthreadB);
system("pause");
return 0;
}
PARAMETERS:
ReleaseSemaphore(hsemaphore, 1, 0);
*Increases the count of the specified semaphore
object by a specified amount.
-A handle to the semaphore object. The
CreateSemaphore or OpenSemaphore function
returns this handle.
-The amount by which the semaphore object's
current count is to be increased. The value must be
greater than zero. If the specified amount would
cause the semaphore's count to exceed the
maximum count that was specified when the
semaphore was created, the count is not changed
and the function returns FALSE.
-A pointer to a variable to receive the previous
count for the semaphore.
hthreadA = CreateThread(NULL, 0, MyFunA, NULL,
0, &threadid);
-If lpThreadAttributes is NULL, the handle cannot be
inherited.If lpThreadAttributes is NULL, the thread
gets a default security descriptor.
-The initial size of the stack, in bytes.If this
parameter is zero, the new thread uses the default
size for the executable.
-is thread p jo funct execute krwana hai us ka name
-agr koi variable pass krna hai tou uska pointer h ye
-flag (0 ka mtlb create hoty hi run hojayega thread)
-thread identifier (kn sa thread h)
hsemaphore = CreateSemaphoreW(NULL, 0, 1,
NULL);
-If lpThreadAttributes is NULL, the handle cannot be
inherited.If lpThreadAttributes is NULL, the thread
gets a default security descriptor.
-The state of a semaphore is non-signaled
-The maximum count for the semaphore object.
This value must be greater than zero.
-If lpName is NULL, the semaphore object is created
without a name.

You might also like