本文整理汇总了C++中pthread_mutexattr_setpshared函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_mutexattr_setpshared函数的具体用法?C++ pthread_mutexattr_setpshared怎么用?C++ pthread_mutexattr_setpshared使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_mutexattr_setpshared函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char **argv)
{
pthread_mutex_t mutex;
pthread_mutexattr_t attr;
int i;
char *processName;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, 0);
pthread_mutex_init(&mutex, &attr);
pid_t pid = fork();
if (pid >= 0) {
if (pid == 0) {
processName = "Child process";
} else {
processName = "Parent process";
}
for (i = 0; i < 10; ++i) {
printf("%s: acquiring mutex.\n", processName);
pthread_mutex_lock(&mutex);
printf("%s: mutex acquired, sleeping.\n", processName);
sleep(1);
printf("%s: Done sleeping, releasing mutex.\n", processName);
pthread_mutex_unlock(&mutex);
printf("%s: Mutex released.\n", processName);
}
} else {
printf("Failed to fork\n");
}
return 0;
}
示例2: tal_init
inline void tal_init(lock_t* lock, lock_attr_t* attr)
{
/* Set attributes */
if (attr != NULL) {
memcpy(&(lock->attr), attr, sizeof(lock_attr_t));
}
#ifdef WITH_POSIX_THREAD_PROCESS_SHARED
if (pthread_mutexattr_init(&(lock->mtx_attr)) != 0) {
fprintf(stderr, "Error: Could not init mutexattr\n");
}
if (pthread_mutexattr_setpshared(&(lock->mtx_attr), PTHREAD_PROCESS_SHARED) != 0) {
fprintf(stderr, "Error: Could not set pshared\n");
}
if (pthread_mutex_init(&(lock->mtx), &(lock->mtx_attr)) != 0) {
fprintf(stderr, "Error: Could not initalize mutex\n");
}
#else
#if LOCK_ROUND_ROBIN == 1
fork_idx = attr->fork_idx;
lock->current_idx = fork_idx;
#else
atomic_flag_clear(&(lock->cat));
#endif
#endif
}
示例3: accept_mutex_init
void accept_mutex_init(void)
{
pthread_mutexattr_t mattr;
int fd;
fd = open ("/dev/zero", O_RDWR);
if (fd == -1) {
perror ("open(/dev/zero)");
exit (1);
}
mutex = (pthread_mutex_t *)mmap ((caddr_t)0, sizeof (*mutex),
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (mutex == (void *)(caddr_t)-1) {
perror ("mmap");
exit (1);
}
close (fd);
if (pthread_mutexattr_init(&mattr)) {
perror ("pthread_mutexattr_init");
exit (1);
}
if (pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED)) {
perror ("pthread_mutexattr_setpshared");
exit (1);
}
if (pthread_mutex_init(mutex, &mattr)) {
perror ("pthread_mutex_init");
exit (1);
}
sigfillset(&accept_block_mask);
sigdelset(&accept_block_mask, SIGHUP);
sigdelset(&accept_block_mask, SIGTERM);
sigdelset(&accept_block_mask, SIGUSR1);
}
示例4: raise
Mutex::Mutex() {
pthread_mutexattr_t attr;
if (int code = pthread_mutexattr_init(&attr)) {
raise(code);
}
if (int code = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE)) {
pthread_mutexattr_destroy(&attr);
raise(code);
}
// if (pthread_mutexattr_settype&attr, PTHREAD_MUTEX_DEFAULT) {
// pthread_mutexattr_destory(&attr);
// throw error(errno);
// }
//
// if (pthread_mutexattr_setprotocol&attr, PTHREAD_PRIO_NONE)) {
// pthread_mutexattr_destory(&attr);
// throw error(errno);
// }
if (int code = pthread_mutex_init(&mutex, &attr)) {
pthread_mutexattr_destroy(&attr);
raise(code);
}
if (int code = pthread_mutexattr_destroy(&attr)) {
raise(code);
}
//print "create";
}
示例5: 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;
}
示例6: main
int main()
{
/* Make sure there is process-shared capability. */
#ifndef PTHREAD_PROCESS_SHARED
fprintf(stderr,"process-shared attribute is not available for testing\n");
return PTS_UNRESOLVED;
#endif
pthread_mutexattr_t mta;
int ret;
/* Set the attribute to INVALID_PSHARED_VALUE. */
ret=pthread_mutexattr_setpshared(&mta, INVALID_PSHARED_VALUE);
if (ret != 0)
{
if (ret == EINVAL)
{
printf("Test PASSED\n");
return PTS_PASS;
}
printf("Test FAILED: Expected return code 0 or EINVAL, got: %d", ret);
return PTS_FAIL;
}
printf("Test PASSED: NOTE*: Returned 0 on error, though standard states 'may' fail.\n");
return PTS_PASS;
}
示例7: init_shared_state
int
init_shared_state (struct uiomux_state * state)
{
struct uiomux_block * block;
pthread_mutexattr_t attr;
pthread_mutex_t * mutex;
struct uio * uio;
int i;
state->num_blocks = UIOMUX_BLOCK_MAX;
pthread_mutexattr_init (&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
for (i = 0; i < UIOMUX_BLOCK_MAX; i++) {
/* Initialize mutex, regardless */
mutex = &state->mutex[i].mutex;
pthread_mutex_init (mutex, &attr);
}
pthread_mutexattr_destroy (&attr);
init_owners_table (state);
return 0;
}
示例8: allocate_shared_segment_mutexes
int allocate_shared_segment_mutexes(shm_daf_t *p, char *errmsg, int max_msg_length)
{
#undef SUBNAME
#define SUBNAME "allocate_shared_segment_mutexes"
char msg[MAX_MSG_LEN];
pthread_mutexattr_t psharedm;
pthread_mutexattr_init(&psharedm);
pthread_mutexattr_setpshared(&psharedm, PTHREAD_PROCESS_SHARED);
if (pthread_mutex_init(&(p->service_cmd_log_object.mp), &psharedm) != 0)
{
sprintf(msg, "%s: problem initialising service cmd log mutex, errno = %d - exiting", SUBNAME, errno);
exit(1);
}
if (pthread_mutex_init(&(p->active_steps_object.mp), NULL) != 0)
{
sprintf(msg, "%s: problem initialising active_steps_object mutex, errno = %d - exiting", SUBNAME, errno);
exit(1);
}
return 0;
}
示例9: 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;
}
}
示例10: 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 );
}
示例11: clockobj_init
int clockobj_init(struct clockobj *clkobj,
unsigned int resolution_ns)
{
pthread_mutexattr_t mattr;
struct timespec now;
int ret;
if (resolution_ns == 0)
return __bt(-EINVAL);
memset(clkobj, 0, sizeof(*clkobj));
ret = __clockobj_set_resolution(clkobj, resolution_ns);
if (ret)
return __bt(ret);
/*
* FIXME: this lock is only used to protect the wallclock
* offset readings from updates. We should replace this by a
* confirmed reading loop.
*/
pthread_mutexattr_init(&mattr);
pthread_mutexattr_settype(&mattr, mutex_type_attribute);
pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_PRIVATE);
ret = __bt(-__RT(pthread_mutex_init(&clkobj->lock, &mattr)));
pthread_mutexattr_destroy(&mattr);
if (ret)
return ret;
__RT(clock_gettime(CLOCK_COPPERPLATE, &now));
timespec_sub(&clkobj->offset, &clkobj->epoch, &now);
return 0;
}
示例12: mutex_init
/*
* Initializes a mutex.
*/
int mutex_init(OS_MUTEX *p_mutex, const char *name)
{
int rc = 0;
if (p_mutex)
{
// set attributes to make mutex reentrant (like windows implementation)
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
// if named make it cross-process safe
if (name)
{
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
// create a shared memory id
int shmid = shmget(atoi(name), sizeof (pthread_mutex_t), IPC_CREAT | 0666);
if (shmid != -1)
{
// attach to the shared memory
pthread_mutex_t *p_tmp = (pthread_mutex_t *)shmat(shmid, NULL, 0);
if (p_tmp)
{
memmove(p_mutex, p_tmp, sizeof (pthread_mutex_t));
}
}
}
// failure when pthread_mutex_init(..) != 0
rc = (pthread_mutex_init((pthread_mutex_t *)p_mutex, &attr) == 0);
}
return rc;
}
示例13: 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;
}
示例14: malloc
/*
* Create new hashmap.
* Size is the size of the number of map slots. Leave it 0
* if you want to use a default value.
*/
struct hashmap *hashmap_create(unsigned int size)
{
struct hashmap *map;
if (size == 0)
size = HASHMAP_DEFAULT_SIZE;
map = (struct hashmap *) malloc(sizeof(struct hashmap));
if (map == NULL)
return NULL;
int table_size = sizeof(struct hashmap_item *) * size;
map->size = size;
map->table = (struct hashmap_item **) malloc(table_size);
if (map->table == NULL) {
free(map);
return NULL;
}
bzero(map->table, table_size);
#ifdef HASHMAP_THREAD_SAVE
pthread_mutexattr_t mattr;
pthread_mutexattr_init(&mattr);
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&map->mutex, &mattr);
#endif /* HASHMAP_THREAD_SAVE */
return map;
}
示例15: swTable_create
int swTable_create(swTable *table)
{
uint32_t row_num = table->size * (1 + SW_TABLE_CONFLICT_PROPORTION);
//header + data
uint32_t row_memory_size = sizeof(swTableRow) + table->item_size;
/**
* row data & header
*/
size_t memory_size = row_num * row_memory_size;
/**
* memory pool for conflict rows
*/
memory_size += sizeof(swMemoryPool) + sizeof(swFixedPool) + ((row_num - table->size) * sizeof(swFixedPool_slice));
/**
* for iterator, Iterate through all the elements
*/
memory_size += table->size * sizeof(swTableRow *);
void *memory = sw_shm_malloc(memory_size);
if (memory == NULL)
{
return SW_ERR;
}
memset(memory, 0, memory_size);
table->memory = memory;
table->compress_threshold = table->size * SW_TABLE_COMPRESS_PROPORTION;
table->rows = memory;
memory += table->size * sizeof(swTableRow *);
memory_size -= table->size * sizeof(swTableRow *);
#if SW_TABLE_USE_SPINLOCK == 0
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
#endif
int i;
for (i = 0; i < table->size; i++)
{
table->rows[i] = memory + (row_memory_size * i);
#if SW_TABLE_USE_SPINLOCK == 0
pthread_mutex_init(&table->rows[i]->lock, &attr);
#endif
}
memory += row_memory_size * table->size;
memory_size -= row_memory_size * table->size;
table->pool = swFixedPool_new2(row_memory_size, memory, memory_size);
return SW_OK;
}