السلام عليكم ورحمة الله وبركاته ....
أخواني اخواتي عندي استفسار أريد مساعدتكم ضروري
ابغى اعرف طريقة استخدام Semaphores في أنظمة Windows وLinux بلغة C .
السلام عليكم ورحمة الله وبركاته ....
أخواني اخواتي عندي استفسار أريد مساعدتكم ضروري
ابغى اعرف طريقة استخدام Semaphores في أنظمة Windows وLinux بلغة C .
سبحان الله وبحمده .... سبحان الله العظيم
الأخ الكريم/الأخت الكريمة
السلام عليكم ورحمة الله وبركاته
مرحباً بكم في منتدى الفريق العربي للبرمجة
نود تنبيهك أن العنوان غير مناسب.
الرجاء اختيار عنوان مناسب أكثر في مواضيعك القادمة، وهذا بمثابة إشعار للأهمية.
وفي حال التكرار سيتعرض موضوعك للإغلاق والحذف!!!
قواعد المشاركة
شاكرين لكم حُسن تعاونكم
تطبيق طمأنينة ، نسخة بيتا على أندرويد
عبدالله الشمّري - Al-Shammari
twitter @abshammeri
abshammeri AT gmail.com
github : abshammeri
هذا مثال لاستخدام الSemaphore من MSDN
#include <windows.h>
#include <stdio.h>
#define MAX_SEM_COUNT 10
#define THREADCOUNT 12
HANDLE ghSemaphore;
DWORD WINAPI ThreadProc( LPVOID );
void main()
{
HANDLE aThread[THREADCOUNT];
DWORD ThreadID;
int i;
// Create a semaphore with initial and max counts of MAX_SEM_COUNT
ghSemaphore = CreateSemaphore(
NULL, // default security attributes
MAX_SEM_COUNT, // initial count
MAX_SEM_COUNT, // maximum count
NULL); // unnamed semaphore
if (ghSemaphore == NULL)
{
printf("CreateSemaphore error: %d\n", GetLastError());
return;
}
// Create worker threads
for( i=0; i < THREADCOUNT; i++ )
{
aThread = CreateThread(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE) ThreadProc,
NULL, // no thread function arguments
0, // default creation flags
&ThreadID); // receive thread identifier
if( aThread == NULL )
{
printf("CreateThread error: %d\n", GetLastError());
return;
}
}
// Wait for all threads to terminate
WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);
// Close thread and semaphore handles
for( i=0; i < THREADCOUNT; i++ )
CloseHandle(aThread);
CloseHandle(ghSemaphore);
}
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
DWORD dwWaitResult;
BOOL bContinue=TRUE;
while(bContinue)
{
// Try to enter the semaphore gate.
dwWaitResult = WaitForSingleObject(
ghSemaphore, // handle to semaphore
0L); // zero-second time-out interval
switch (dwWaitResult)
{
// The semaphore object was signaled.
case WAIT_OBJECT_0:
// TODO: Perform task
printf("Thread %d: wait succeeded\n", GetCurrentThreadId());
bContinue=FALSE;
// Simulate thread spending time on task
Sleep(5);
// Release the semaphore when task is finished
if (!ReleaseSemaphore(
ghSemaphore, // handle to semaphore
1, // increase count by one
NULL) ) // not interested in previous count
{
printf("ReleaseSemaphore error: %d\n", GetLastError());
}
break;
// The semaphore was nonsignaled, so a time-out occurred.
case WAIT_TIMEOUT:
printf("Thread %d: wait timed out\n", GetCurrentThreadId());
break;
}
}
return TRUE;
}المصدر
http://msdn.microsoft.com/en-us/library/ms686946(VS.85).aspx
مصري في بلاد الفرنجة.
قريباً اقرأ مقالاتي على It-scoop
المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية
جارٍ التحقق من المتواجدين…