本文整理汇总了C++中CreateSemaphore函数的典型用法代码示例。如果您正苦于以下问题:C++ CreateSemaphore函数的具体用法?C++ CreateSemaphore怎么用?C++ CreateSemaphore使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CreateSemaphore函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: acl_mymalloc
/* Create a semaphore */
ACL_SEM *acl_sem_create2(const char *pathname, unsigned int initial_value)
{
const char *myname = "acl_sem_create2";
ACL_SEM *sem;
/* Allocate sem memory */
sem = (ACL_SEM *) acl_mymalloc(sizeof(*sem));
if (sem == NULL) {
acl_msg_error("%s, %s(%d): malloc error(%s)",
__FILE__, myname, __LINE__, acl_last_serror());
return NULL;
}
/* Create the semaphore, with max value 32K */
sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, pathname);
sem->count = initial_value;
if (!sem->id) {
acl_msg_error("%s, %s(%d): Couldn't create semaphore(%s)",
__FILE__, myname, __LINE__, acl_last_serror());
acl_myfree(sem);
return NULL;
}
return sem;
}
示例2: pvinit_semaphore
int pvinit_semaphore(WSEMAPHORE *s, int cmax)
{
/* Create a semaphore with initial count=0 max. counts of cmax. */
#ifdef PVWIN32THREAD
s->cmax = cmax;
s->hSemaphore = CreateSemaphore(
NULL, /* no security attributes */
0, /* initial count */
cmax, /* maximum count */
NULL); /* unnamed semaphore */
if(s->hSemaphore == NULL) return -1; /* Check for error. */
return 0;
#else
s->cmax = cmax;
s->nready = 0;
pvthread_mutex_init(&s->mutex, NULL);
return 0;
#endif
}
示例3: pthread_cond_init
int pthread_cond_init(pthread_cond_t *cond, const void *unused) {
REDIS_NOTUSED(unused);
cond->waiters = 0;
cond->was_broadcast = 0;
InitializeCriticalSection(&cond->waiters_lock);
cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
if (!cond->sema) {
errno = GetLastError();
return -1;
}
cond->continue_broadcast = CreateEvent(NULL, /* security */
FALSE, /* auto-reset */
FALSE, /* not signaled */
NULL); /* name */
if (!cond->continue_broadcast) {
errno = GetLastError();
return -1;
}
return 0;
}
示例4: MainThread
DWORD WINAPI MainThread(void* pData)
{
MAIN_THREAD_DATA_STRUCT* mtds = (MAIN_THREAD_DATA_STRUCT*)pData;
CRIFFChunkTreeDlg* dlg = mtds->dlg;
STREAM* source = mtds->source;
__int64 qwPos=source->GetPos();
dwMBNbr=0;
LISTHEADER lh;
source->Seek(0);
while (!source->IsEndOfStream()) {
source->Read(&lh,12);
HANDLE h = CreateSemaphore(NULL, 0, 1, NULL);
StartInsertListThread(dlg, NULL, &lh, source->GetPos() - 12, h);
WaitForSingleObject(h, INFINITE);
CloseHandle(h);
}
source->Seek(qwPos);
delete mtds;
return 1;
}
示例5: OsNetworkSetInterfaceChangedObserver
void OsNetworkSetInterfaceChangedObserver(OsContext* aContext, InterfaceListChanged aCallback, void* aArg)
{
InterfaceChangeObserver* icobs;
if (NULL != aContext->iInterfaceChangeObserver) {
return;
}
icobs = (InterfaceChangeObserver*)malloc(sizeof(InterfaceChangeObserver));
if (NULL == icobs) {
return;
}
icobs->iSocket = socket(AF_INET, SOCK_DGRAM, 0);
SetSocketBlocking(icobs->iSocket);
icobs->iEvent = WSACreateEvent();
icobs->iShutdownEvent = WSACreateEvent();
icobs->iCallback = aCallback;
icobs->iArg = aArg;
icobs->iShutdown = 0;
icobs->iSem = CreateSemaphore(NULL, 0, INT32_MAX, NULL);
(void)WSAEventSelect(icobs->iSocket, icobs->iEvent, FD_ADDRESS_LIST_CHANGE);
(void)CreateThread(NULL, 16*1024, (LPTHREAD_START_ROUTINE)&interfaceChangeThread, icobs, 0, NULL);
aContext->iInterfaceChangeObserver = icobs;
}
示例6: WcmSemaphore_Create
// -----------------------------------------------------------------------------------------
// Semaphore functions
// -----------------------------------------------------------------------------------------
WcmSemaphore_t WcmSemaphore_Create(void)
{
#ifdef WIN32
return CreateSemaphore(NULL, 0, 1000, NULL);
#endif
#ifdef LINUX_OR_OSX
WcmSemaphore_t hSem;
if ((hSem = WcmMalloc(sizeof(stWcmSemaphore_t))) == NULL)
return NULL;
hSem->u32Count = 0;
if(pthread_mutex_init(&hSem->hMutex, NULL) != 0)
{
WcmFree(hSem);
return NULL;
}
if(pthread_cond_init(&hSem->condSem, NULL) != 0)
{
pthread_mutex_destroy(&hSem->hMutex);
WcmFree(hSem);
return NULL;
}
return hSem;
#endif
} /* WcmSemaphore_Create */
示例7: SemMP_create
/*
* ======== SemMP_create ========
*/
SemMP_Handle SemMP_create(Int key, Int count)
{
HANDLE mutexId;
HANDLE semId;
SemMP_Obj *sem;
TCHAR tszKey[MAX_PATH + 1] = TEXT("");
GT_2trace(curTrace, GT_ENTER, "SemMP_create> key: 0x%x count: %d\n", key,
count);
if ((sem = (SemMP_Obj *)Memory_alloc(sizeof(SemMP_Obj), NULL)) == NULL) {
GT_0trace(curTrace, GT_7CLASS, "SemMP_create> Memory_alloc failed\n");
return (NULL);
}
_stprintf(tszKey, TEXT("%d"), key);
sem->id = CreateSemaphore(NULL, count, count, tszKey);
GT_1trace(curTrace, GT_ENTER, "SemMP_create> semId: 0x%x\n", sem->id);
GT_1trace(curTrace, GT_ENTER, "Leaving SemMP_create> sem[0x%x]\n", sem);
return (sem);
}
示例8: ipcon_device_create
void ipcon_device_create(Device *device, const char *uid) {
int i;
for(i = 0; i < MAX_NUM_CALLBACKS; i++) {
device->registered_callbacks[i] = NULL;
device->callback_wrappers[i] = NULL;
}
device->uid = ipcon_base58decode(uid);
device->ipcon = NULL;
device->response.function_id = 0;
device->response.length = 0;
#ifdef _WIN32
InitializeCriticalSection(&device->write_mutex);
// Default state for response semaphore is empty
device->response_semaphore = CreateSemaphore(NULL, 0, 1, NULL);
#else
pthread_mutex_init(&device->write_mutex, NULL);
pthread_mutex_init(&device->response_mutex, NULL);
pthread_cond_init(&device->response_cond, NULL);
device->response_flag = false;
#endif
}
示例9: theSemaphoreHandle
Sem::Sem(const char* const inName,
const unsigned long inCount,
const unsigned long inMaxCount)
: theSemaphoreHandle(0),
theMaxCount(inMaxCount)
{
if (theMaxCount == 0 || theMaxCount > 0x7FFFFFFF)
{
theMaxCount = 0x7FFFFFFF;
}
theSemaphoreHandle = CreateSemaphore(NULL,
inCount > theMaxCount ? theMaxCount : inCount,
theMaxCount,
inName);
if (theSemaphoreHandle == NULL)
{
DWORD rc = GetLastError();
if (rc == ERROR_ALREADY_EXISTS)
{
// semaphore is creted try to open it
theSemaphoreHandle = OpenSemaphore(SEMAPHORE_ALL_ACCESS | SYNCHRONIZE, FALSE, inName);
if (theSemaphoreHandle == 0)
{
Logger::abort("Semaphore: failed to open.");
}
}
else
{
Logger::abort("Semaphore: failed to create.");
}
}
}
示例10: OS_CreateMB
/*-------------------------------------------
| Name:OS_CreateMB
| Description:
| Parameters:
| Return Type:
| Comments:
| See:
---------------------------------------------*/
void OS_CreateMB (OS_MAILBOX* pMB, OS_U8 sizeofMsg, OS_UINT maxnofMsg, void* Buffer){
if(!pMB)return;
pMB->hSemMB = CreateSemaphore (NULL,1,1,NULL);
pMB->hEvtMB = CreateEvent(NULL,0,0,NULL);
pMB->dwThreadId = 0;
pMB->pData = Buffer;
pMB->pRead = pMB->pData;
pMB->pWrite = pMB->pData;
pMB->pEnd = pMB->pData + ((maxnofMsg-1)*sizeofMsg);
pMB->maxnofMsg = maxnofMsg;
pMB->sizeofMsg = sizeofMsg;
pMB->r = 0;
pMB->w = 0;
pMB->size = maxnofMsg*sizeofMsg;
}
示例11: sys_mbox_new
sys_mbox_t sys_mbox_new(int size)
{
struct lwip_mbox *new_mbox;
LWIP_UNUSED_ARG(size);
new_mbox = (struct lwip_mbox*)malloc(sizeof(struct lwip_mbox));
LWIP_ASSERT("new_mbox != NULL", new_mbox != NULL);
if(new_mbox == NULL) {
#if LWIP_STATS
lwip_stats.sys.mbox.err++;
#endif /* LWIP_STATS */
return SYS_SEM_NULL;
}
new_mbox->sem = CreateSemaphore(0, 0, MAX_QUEUE_ENTRIES, 0);
LWIP_ASSERT("Error creating semaphore", new_mbox->sem != NULL);
if(new_mbox->sem == NULL) {
#if LWIP_STATS
lwip_stats.sys.mbox.err++;
#endif /* LWIP_STATS */
free(new_mbox);
new_mbox = NULL;
return SYS_SEM_NULL;
}
memset(&new_mbox->q_mem, 0, sizeof(u32_t)*MAX_QUEUE_ENTRIES);
new_mbox->head = 0;
new_mbox->tail = 0;
#if LWIP_STATS
lwip_stats.sys.mbox.used++;
LWIP_ASSERT("sys_mbox_new() counter overflow", lwip_stats.sys.mbox.used != 0 );
if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max) {
lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
}
#endif /* LWIP_STATS */
return new_mbox;
}
示例12: defined
/* Create a semaphore */
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_sem *sem;
/* Allocate sem memory */
sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
if ( sem ) {
/* Create the semaphore, with max value 32K */
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
sem->id = CreateSemaphoreCE(NULL, initial_value, 32*1024, NULL);
#else
sem->id = CreateSemaphore(NULL, initial_value, 32*1024, NULL);
#endif
sem->count = initial_value;
if ( ! sem->id ) {
SDL_SetError("Couldn't create semaphore");
SDL_free(sem);
sem = NULL;
}
} else {
SDL_OutOfMemory();
}
return(sem);
}
示例13: mutex_create
mutex_t mutex_create(void)
{
mutex_t m;
m = (mutex_t) malloc(sizeof(struct mutex_s));
if(m == NULL)
return NULL;
#if defined(OS_WINDOWS)
m->mutex = CreateSemaphore(NULL, 1, 1, NULL);
if(m->mutex == NULL)
goto fail_mutex;
#else
pthread_mutex_init(&(m->mutex), NULL);
#endif
return m;
#if defined(OS_WINDOWS)
fail_mutex:
free(m);
return NULL;
#endif
}
示例14: rktio_winsock_init
int rktio_winsock_init(rktio_t *rktio)
{
if (!winsock_sema) {
winsock_sema = CreateSemaphore(NULL, 1, 1, NULL);
}
WaitForSingleObject(winsock_sema, INFINITE);
if (!winsock_started) {
WSADATA data;
if (!WSAStartup(MAKEWORD(1, 1), &data)) {
winsock_started = 1;
} else {
get_windows_error();
ReleaseSemaphore(winsock_sema, 1, NULL);
return 0;
}
} else
winsock_started++;
ReleaseSemaphore(winsock_sema, 1, NULL);
return 1;
}
示例15: main
//.........这里部分代码省略.........
gethostname(host_name, sizeof(host_name));
hp = gethostbyname(host_name);
/* Check for NULL pointer */
if (hp == NULL)
{
fprintf(stderr, "Could not get host name.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
/* Assign the address */
server.sin_addr.S_un.S_un_b.s_b1 = hp->h_addr_list[0][0];
server.sin_addr.S_un.S_un_b.s_b2 = hp->h_addr_list[0][1];
server.sin_addr.S_un.S_un_b.s_b3 = hp->h_addr_list[0][2];
server.sin_addr.S_un.S_un_b.s_b4 = hp->h_addr_list[0][3];
}
/* Otherwise assign it manually */
else
{
server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;
}
/* Bind address to socket */
if (bind(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Could not bind name to socket.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
/* Print out server information */
printf("Binding pada %u.%u.%u.%u : %u\n", (unsigned char)server.sin_addr.S_un.S_un_b.s_b1,
(unsigned char)server.sin_addr.S_un.S_un_b.s_b2,
(unsigned char)server.sin_addr.S_un.S_un_b.s_b3,
(unsigned char)server.sin_addr.S_un.S_un_b.s_b4,
(unsigned short) port_number);
printf("Press CTRL + C to quit\n");
/* Loop and get data from clients */
// 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 1;
}
// Create 2 worker threads aThread[0] for reading and sending file
aThread[0] = CreateThread(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE) ProcRcvData,
NULL, // no thread function arguments
0, // default creation flags
&ThreadID); // receive thread identifier
//case of making thread error
if( aThread[0] == NULL )
{
printf("CreateThread error: %d\n", GetLastError());
return 1;
}
//aThread[1] for listening XON and XOFF
aThread[1] = CreateThread(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE) ProcWaitSignal,
NULL, // no thread function arguments
0, // default creation flags
&ThreadID); // receive thread identifier
//case of making thread error
if( aThread[1] == NULL )
{
printf("CreateThread error: %d\n", GetLastError());
return 1;
}
// Wait for all threads to terminate
WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);
// Close thread and semaphore handles
for( i=0; i < THREADCOUNT; i++ )
CloseHandle(aThread[i]);
CloseHandle(ghSemaphore);
return 0;
}