当前位置: 首页>>代码示例>>C++>>正文


C++ pthread_condattr_setpshared函数代码示例

本文整理汇总了C++中pthread_condattr_setpshared函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_condattr_setpshared函数的具体用法?C++ pthread_condattr_setpshared怎么用?C++ pthread_condattr_setpshared使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了pthread_condattr_setpshared函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc, char* argv[]) {
    pid_t child_id;

    //shared memory

    key_t memKey = 6655; /* A key to access shared memory segments */
    int memSize = sizeof(SharedMem);
    int memFlag = 1023; /* Call permissions and modes are set. */
    sharingMemory(memKey, memSize, memFlag);

    /*shared mutex*/
    pthread_mutexattr_t attr1, attr2;
    pthread_mutexattr_setpshared (&attr1, PTHREAD_PROCESS_SHARED);
    pthread_mutexattr_setpshared (&attr2, PTHREAD_PROCESS_SHARED);
    pthread_mutex_init (&shmem_ptr->lock, &attr1);
    pthread_mutex_init (&shmem_ptr->lock2, &attr2);

    /*shared conditions*/
    pthread_condattr_t spaceAttr, itemAttr;
    pthread_condattr_setpshared(&spaceAttr, PTHREAD_PROCESS_SHARED);
    pthread_condattr_setpshared(&itemAttr, PTHREAD_PROCESS_SHARED);
    pthread_cond_init(&shmem_ptr->SpaceAvailable, &spaceAttr);
    pthread_cond_init(&shmem_ptr->ItemAvailable, &itemAttr);

    //split code

    if ((child_id = fork()) == -1) {
        printf("Could not fork()\n");
        exit(1);
    }
    if (child_id != 0) { //consumer
      pid_t child2_id;
      if ((child2_id = fork()) == -1) {
          printf("Could not fork()\n");
          exit(1);
      }
      if(child2_id != 0){ //consumer
          char keystr[10];
          sprintf(keystr,"%d", memKey);
          execl("./consum", "consum", keystr, NULL);
          /* done with the program, so detach the shared segment and terminate */
          shmdt ( (void *)  shmem_ptr);
          printf("Finished execution \n");
      }else{ //pblack
          char keystr[10];
          sprintf(keystr,"%d", memKey);
          execl("./pBlack", "pBlack", keystr, NULL);
      }
    }else{ //pgreen
        char keystr[10];
        sprintf(keystr,"%d", memKey);
        execl("./pGreen", "pGreen", keystr, NULL);
    }


    return 0;
}
开发者ID:miguelpfitscher,项目名称:OS,代码行数:57,代码来源:main.c

示例2: create_shm_channel

shm_data* create_shm_channel(char* fldes) {
		shm_unlink(fldes);
//		fprintf(stdout, "fldes is: %s.\n", fldes);
        int shm_fldes = shm_open(fldes, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
        if (shm_fldes < 0) {
                fprintf(stderr, "Error %d (%s) on server proxy shm_open.\n", errno, strerror(errno));
				fflush(stdout);
 //               exit(1);
        }

        ftruncate(shm_fldes, segment_size);
        shm_data* mem = mmap(NULL, segment_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fldes, 0);
        close(shm_fldes);

        if(mem->file_length != 0 || mem->bytes_written != 0) {
          fprintf(stderr, "shared memory segment should have been set zero.\n");
		  fflush(stdout);
   //       exit(1);
        }


        mem->file_length = 0;
        mem->bytes_written = 0;

// initialize shared memory mutex and condition variables		
		
        pthread_mutexattr_t memory_attr;
		pthread_mutexattr_init(&memory_attr);
        pthread_mutexattr_setpshared(&memory_attr, PTHREAD_PROCESS_SHARED);
        pthread_mutex_init(&mem->data_mutex, &memory_attr);
		
		pthread_mutexattr_t filelen_attr;
		pthread_mutexattr_init(&filelen_attr);
        pthread_mutexattr_setpshared(&filelen_attr, PTHREAD_PROCESS_SHARED);
        pthread_mutex_init(&mem->file_len_mutex, &filelen_attr);
		
        pthread_condattr_t cache_attr;
		pthread_condattr_init(&cache_attr);
        pthread_condattr_setpshared(&cache_attr, PTHREAD_PROCESS_SHARED);
        pthread_cond_init(&mem->cache_cond, &cache_attr);
		
		pthread_condattr_t proxy_attr;
		pthread_condattr_init(&proxy_attr);
        pthread_condattr_setpshared(&proxy_attr, PTHREAD_PROCESS_SHARED);
        pthread_cond_init(&mem->proxy_cond, &proxy_attr);
        
        return mem;
}
开发者ID:kevinajordan,项目名称:CS8803_Introduction_to_OS,代码行数:48,代码来源:handle_with_cache.c

示例3: barrier_init

int barrier_init(barrier_t *barrier,int needed)
{
    barrier->needed = needed;
    barrier->called = 0;

	if(with_fork){
		pthread_condattr_t cattr;
		pthread_mutexattr_t mattr;

		pthread_mutexattr_init(&mattr);
		if(pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED)){
			perror("Mutex PTHREAD_PROCESS_SHARED");
			exit(1);
		}

	    pthread_condattr_init(&cattr);
		if(pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED)){
			perror("Cond PTHREAD_PROCESS_SHARED");
			exit(1);
		}

		pthread_mutex_init(&barrier->mutex,&mattr);
	    pthread_cond_init(&barrier->cond, &cattr);
	}else{
	    pthread_mutex_init(&barrier->mutex,NULL);
	    pthread_cond_init(&barrier->cond,NULL);
	}
    return 0;
}
开发者ID:formyfamily,项目名称:ski,代码行数:29,代码来源:ski-barriers.c

示例4: create_locks

static ledger_status create_locks(ledger_partition *partition, int fd) {
    ledger_status rc;
    ledger_partition_locks locks;
    pthread_mutexattr_t mattr;
    pthread_condattr_t cattr;

    rc = pthread_mutexattr_init(&mattr);
    ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to initialize mutex attribute");

    rc = pthread_condattr_init(&cattr);
    ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to initialize cond attribute");

    rc = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
    ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to set mutex attribute to shared");

    rc = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
    ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to set cond attribute to shared");

    rc = pthread_mutex_init(&locks.rotate_lock, &mattr);
    ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to initialize journal rotate mutex");

    rc = pthread_cond_init(&locks.rotate_cond, &cattr);
    ledger_check_rc(rc == 0, LEDGER_ERR_GENERAL, "Failed to initialize journal rotate cond");

    rc = ledger_pwrite(fd, (void *)&locks, sizeof(ledger_partition_locks), 0);
    ledger_check_rc(rc, LEDGER_ERR_IO, "Failed to write meta number of entries");

    return LEDGER_OK;

error:
    return rc;
}
开发者ID:blakesmith,项目名称:ledgerd,代码行数:32,代码来源:partition.c

示例5: TEST

TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
#if defined(__BIONIC__) // This tests a bionic implementation detail.
  pthread_condattr_t attr;
  pthread_condattr_init(&attr);

  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
  ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));

  pthread_cond_t cond_var;
  ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));

  ASSERT_EQ(0, pthread_cond_signal(&cond_var));
  ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));

  attr = static_cast<pthread_condattr_t>(cond_var.value);
  clockid_t clock;
  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
  ASSERT_EQ(CLOCK_MONOTONIC, clock);
  int pshared;
  ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
  ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
#else // __BIONIC__
  GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif // __BIONIC__
}
开发者ID:nick0lay,项目名称:platform_bionic,代码行数:25,代码来源:pthread_test.cpp

示例6: uv_cond_init

int uv_cond_init(uv_cond_t* cond) {
  pthread_condattr_t attr;

  if (pthread_condattr_init(&attr))
    return -1;
#ifndef __ANDROID__
  if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
#else
  if (pthread_condattr_setpshared(&attr, CLOCK_MONOTONIC))
#endif
    goto error2;

  if (pthread_cond_init(cond, &attr))
    goto error2;

  if (pthread_condattr_destroy(&attr))
    goto error;

  return 0;

error:
  pthread_cond_destroy(cond);
error2:
  pthread_condattr_destroy(&attr);
  return -1;
}
开发者ID:559210,项目名称:libpomelo,代码行数:26,代码来源:thread.c

示例7: init_ring_buffer

void init_ring_buffer(struct ring_buffer* rbuff) {
  rbuff->request_writes = 0;
  rbuff->request_reads = 0;

  rbuff->response_writes = 0;
  rbuff->response_reads = 0;

  pthread_mutexattr_t mattr;
  pthread_mutexattr_init(&mattr);
  pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);  
  pthread_mutex_init(&rbuff->data_mutex, &mattr);
  
  pthread_condattr_t  cattr;
  pthread_condattr_init(&cattr);
  pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
  pthread_cond_init(&rbuff->nonempty, &cattr);
  

  int i;
  for(i = 0; i < MAXSIZE; i++) {
    pthread_cond_init(&rbuff->response_ready[i], &cattr);
    rbuff->buffer[i].request_client.response = i;
  }

}
开发者ID:jedivind,项目名称:xen,代码行数:25,代码来源:ringbuffer.c

示例8: epicsMutexOsdCreate

epicsMutexOSD * epicsMutexOsdCreate(void) {
    epicsMutexOSD *pmutex;
    int           status;

    pmutex = callocMustSucceed(1, sizeof(*pmutex), "epicsMutexOsdCreate");
    status = pthread_mutexattr_init(&pmutex->mutexAttr);
    checkStatusQuit(status, "pthread_mutexattr_init", "epicsMutexOsdCreate");

#if defined _POSIX_THREAD_PRIO_INHERIT
    status = pthread_mutexattr_setprotocol(
        &pmutex->mutexAttr,PTHREAD_PRIO_INHERIT);
    if (errVerbose) checkStatus(status, "pthread_mutexattr_setprotocal");
#endif /*_POSIX_THREAD_PRIO_INHERIT*/

    status = pthread_mutex_init(&pmutex->lock, &pmutex->mutexAttr);
    checkStatusQuit(status, "pthread_mutex_init", "epicsMutexOsdCreate");

#if defined _POSIX_THREAD_PROCESS_SHARED
    status = pthread_condattr_init(&pmutex->condAttr);
    checkStatus(status, "pthread_condattr_init");
    status = pthread_condattr_setpshared(&pmutex->condAttr,
        PTHREAD_PROCESS_PRIVATE);
    checkStatus(status, "pthread_condattr_setpshared");
    status = pthread_cond_init(&pmutex->waitToBeOwner, &pmutex->condAttr);
#else
    status = pthread_cond_init(&pmutex->waitToBeOwner, 0);
#endif /*_POSIX_THREAD_PROCESS_SHARED*/
    checkStatusQuit(status, "pthread_cond_init", "epicsMutexOsdCreate");
    return pmutex;
}
开发者ID:ukaea,项目名称:epics,代码行数:30,代码来源:osdMutex.c

示例9: shm_init_header

/* init shm header */
void shm_init_header( ssm_header *header, int data_size, int history_num, ssmTimeT cycle )
{
	pthread_mutexattr_t mattr;
	pthread_condattr_t cattr; 

	header->tid_top = SSM_TID_SP - 1;	/* 初期位置 */
	header->size = data_size;	/* データサイズ */
	header->num = history_num;	/* 履歴数 */
	//header->table_size = hsize;	/* テーブルサイズ */
	header->cycle = cycle;	/* データ最小サイクル */
	header->data_off = sizeof( ssm_header );	/* データの先頭アドレス */
	header->times_off = header->data_off + ( data_size * history_num );	/* 時刻の先頭アドレス */
	//header->table_off = header->times_off + sizeof( ssmTimeT ) * hsize ;	/* time tableの先頭アドレス */
	
	/* 同期用mutexの初期化 */
	pthread_mutexattr_init(&mattr);
	pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
	pthread_mutex_init(&header->mutex, &mattr); 

	pthread_condattr_init(&cattr);
	pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
	pthread_cond_init(&header->cond, &cattr);
	
	pthread_mutexattr_destroy( &mattr );
	pthread_condattr_destroy( &cattr );
}
开发者ID:anjinkristou,项目名称:SSM,代码行数:27,代码来源:libssm-shm.c

示例10: main

int main(){
	
	pthread_mutexattr_t mutex_attr;
	pthread_condattr_t cond_attr;
	buffer.occupied = buffer.nextin = buffer.nextout = 0;
	
	pthread_mutexattr_init(&mutex_attr);
	pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
	pthread_mutex_init(&buffer.lock, &mutex_attr);
	
	pthread_condattr_init(&cond_attr);
	pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
	pthread_cond_init(&buffer.less, &cond_attr);
	pthread_cond_init(&buffer.more, &cond_attr);
	
	
	pthread_t cons, prod;
	int idCons = 0, idProd = 1;
	
	pthread_create(&cons, NULL, (void *) conscons, (void *) &idCons);
	pthread_create(&prod, NULL, (void *) prodprod, (void *) &idProd);
	
	pthread_join(cons, NULL);
	pthread_join(prod, NULL);
	
	return 0;
}
开发者ID:diega,项目名称:tp-sistemas-operativos,代码行数:27,代码来源:threads.c

示例11: mCounter

CBinarySemaphore::CBinarySemaphore(bool isFull, bool isProcessShared) : mCounter(1)
{
	Int32 retVal;
	pthread_mutexattr_t mutexAttr;
	retVal = pthread_mutexattr_init(&mutexAttr);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to init Mutex-Attribute!");

	retVal = pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_ERRORCHECK);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to set Mutex-Type!");

	retVal = pthread_mutex_init(&mMutex, &mutexAttr);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to init Mutex!");

	retVal = pthread_mutexattr_destroy(&mutexAttr);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to destroy Mutex-Attribute!");

	pthread_condattr_t conditionAttr;
	retVal = pthread_condattr_init(&conditionAttr);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to init Condition-Attribute!");

	retVal = pthread_condattr_setpshared(&conditionAttr,
										 isProcessShared ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to set Condition-Attribute!");

	pthread_cond_init(&mCondition, &conditionAttr);
	pthread_condattr_destroy(&conditionAttr);


	if(false == isFull)
	{
		mCounter = 0;
	}
}
开发者ID:HSKA-CuBa,项目名称:CuBa_git,代码行数:33,代码来源:CBinarySemaphore.cpp

示例12: fifo_init

int fifo_init(struct fifo *fifo, size_t size, bool_t prio_inherit) {
#if defined(HAVE_LIBPTHREAD) && !defined(SDL_FIFOS)

#ifndef _POSIX_THREAD_PROCESS_SHARED
#error "no _POSIX_THREAD_PROCESS_SHARED"
#endif /* _POSIX_THREAD_PROCESS_SHARED */

	/* linux multi-process locking */
	pthread_mutexattr_t mutex_attr;
	pthread_condattr_t cond_attr;
	struct utsname utsname;
	int err;

	if ((err = pthread_mutexattr_init(&mutex_attr)) < 0) {
		return err;
	}
	if (prio_inherit) {
		if ((err = pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED)) < 0) {
			return err;
		}
#ifdef _POSIX_THREAD_PRIO_INHERIT
		/* only on PREEMPT kernels */
		if ((err = uname(&utsname)) < 0) {
			return err;
		}
		if (!RUNNING_ON_VALGRIND && strstr(utsname.version, "PREEMPT") != NULL) {
			if ((err = pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_INHERIT)) < 0) {
				return err;
			}
		}
#endif /* _POSIX_THREAD_PRIO_INHERIT */
	}
	if ((err = pthread_mutex_init(&fifo->mutex, &mutex_attr)) < 0) {
		return err;
	}

	if ((err = pthread_condattr_init(&cond_attr)) < 0) {
		return err;
	}
	if (prio_inherit) {
		if ((err = pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED)) < 0) {
			return err;
		}
	}
	if ((err = pthread_cond_init(&fifo->cond, &cond_attr)) < 0) {
		return err;
	}
#else
	/* cross platform locks */
	fifo->mutex = SDL_CreateMutex();
	fifo->cond = SDL_CreateCond();
#endif
	fifo->rptr = 0;
	fifo->wptr = 0;
	fifo->size = size;

	return 0;
}
开发者ID:gnalbandian,项目名称:squeezeplay,代码行数:58,代码来源:fifo.c

示例13: os_condInit

/** \brief Initialize the condition variable taking the condition
 *         attributes into account
 *
 * \b os_condInit calls \b pthread_cond_init to intialize the posix condition
 * variable.
 *
 * In case the scope attribute is \b OS_SCOPE_SHARED, the posix
 * condition variable "pshared" attribute is set to \b PTHREAD_PROCESS_SHARED
 * otherwise it is set to \b PTHREAD_PROCESS_PRIVATE.
 */
os_result
os_condInit (
    os_cond *cond, 
    os_mutex *dummymtx,
    const os_condAttr *condAttr)
{
    pthread_condattr_t mattr;
    int result = 0;
    os_result rv;

    assert (cond != NULL);
    assert (condAttr != NULL);

#ifdef OSPL_STRICT_MEM
   assert(cond->signature != OS_COND_MAGIC_SIG);
#endif

    pthread_condattr_init (&mattr);
    if (condAttr->scopeAttr == OS_SCOPE_SHARED) {
        result = pthread_condattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED);
    } else {
        result = pthread_condattr_setpshared (&mattr, PTHREAD_PROCESS_PRIVATE);
    }
    if (result == 0) {
#ifdef OSPL_STRICT_MEM
       result = pthread_cond_init (&cond->cond, &mattr);
#else
       result = pthread_cond_init (cond, &mattr);
       if (result == EBUSY) {
          os_condDestroy (cond);
          result = pthread_cond_init (cond, &mattr);
       }
#endif
    }
    pthread_condattr_destroy (&mattr);
    if (result == 0) {
#ifdef OSPL_STRICT_MEM
        cond->signature = OS_COND_MAGIC_SIG;
#endif
	rv = os_resultSuccess;
    } else {
	rv = os_resultFail;
    }
    return rv;
}
开发者ID:xrl,项目名称:opensplice_dds,代码行数:55,代码来源:os_cond.c

示例14: Pthread_condattr_setpshared

void Pthread_condattr_setpshared(pthread_condattr_t * attr, int flag)
{
	int n;

	if ((n = pthread_condattr_setpshared(attr, flag)) == 0)
		return;
	errno = n;
	fprintf(stderr, "pthread_condattr_setpshared error");
}
开发者ID:iloveloveyou,项目名称:chirico,代码行数:9,代码来源:wrapper.c

示例15: pthread_cond_init_shared

int pthread_cond_init_shared ( pthread_cond_t **cond_p )
{
	static pthread_cond_t cond_initial = PTHREAD_COND_INITIALIZER;
	*cond_p = shm_malloc ( sizeof ( **cond_p ) );
	memcpy ( *cond_p, &cond_initial, sizeof ( cond_initial ) );
	pthread_condattr_t attr;
	pthread_condattr_init ( &attr );
	pthread_condattr_setpshared ( &attr, PTHREAD_PROCESS_SHARED );
	return pthread_cond_init ( *cond_p, &attr );
}
开发者ID:xaionaro,项目名称:kvm-pool,代码行数:10,代码来源:pthreadex.c


注:本文中的pthread_condattr_setpshared函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。