• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ pthread_mutexattr_setpshared函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



在下文中一共展示了pthread_mutexattr_setpshared函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
开发者ID:ArnaudCourbiere,项目名称:DataStructures,代码行数:35,代码来源:main.c


示例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
}
开发者ID:IAIK,项目名称:armageddon,代码行数:28,代码来源:lock.c


示例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);
}
开发者ID:Garridon,项目名称:windowsrtdev,代码行数:34,代码来源:time-sem.c


示例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";
        }
开发者ID:adob,项目名称:axe,代码行数:32,代码来源:mutex.cpp


示例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;
}
开发者ID:blakesmith,项目名称:ledgerd,代码行数:32,代码来源:partition.c


示例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;

}
开发者ID:Mellanox,项目名称:arc_ltp,代码行数:30,代码来源:3-2.c


示例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;
}
开发者ID:kfish,项目名称:libuiomux,代码行数:26,代码来源:state.c


示例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;

}
开发者ID:DaveSinclair,项目名称:DAF,代码行数:27,代码来源:daf_shared_memory.c


示例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;
  }

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


示例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 );
}
开发者ID:anjinkristou,项目名称:SSM,代码行数:27,代码来源:libssm-shm.c


示例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;
}
开发者ID:ChunHungLiu,项目名称:xenomai,代码行数:34,代码来源:clockobj.c


示例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;
}
开发者ID:jubalh,项目名称:IXPDIMMSW,代码行数:36,代码来源:lnx_os.c


示例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;
}
开发者ID:diega,项目名称:tp-sistemas-operativos,代码行数:27,代码来源:threads.c


示例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;
}
开发者ID:adavydow,项目名称:scheme-maker-we,代码行数:35,代码来源:hashmap.c


示例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;
}
开发者ID:netcode,项目名称:swoole-src,代码行数:59,代码来源:Table.c


示例16: mutexattr_wrapper

 //!Constructor
 mutexattr_wrapper(bool recursive = false)
 {
    if(pthread_mutexattr_init(&m_attr)!=0 ||
       pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0 ||
        (recursive &&
         pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE)!= 0 ))
       throw interprocess_exception("pthread_mutexattr_xxxx failed");
 }
开发者ID:AsherBond,项目名称:PDAL,代码行数:9,代码来源:pthread_helpers.hpp


示例17: __darwin_pthread_mutexattr_setpshared

int __darwin_pthread_mutexattr_setpshared(pthread_mutexattr_t* attr, int pshared)
{
	pshared = TranslatePshared(pshared);
	int rv = pthread_mutexattr_setpshared(attr, pshared);
	if (rv == -1)
		errnoOut();
	return rv;
}
开发者ID:eccstartup,项目名称:darling,代码行数:8,代码来源:pthread.cpp


示例18: fprintf

bool CMutexLock::MutexSetProcessShare()
{
	if(!m_pstAttr) {
		fprintf(stderr, "m_pstAttr is NULL.\n");
		return false;
	}
	return !pthread_mutexattr_setpshared((pthread_mutexattr_t*)m_pstAttr, PTHREAD_PROCESS_SHARED);
}
开发者ID:asiainfoLDP,项目名称:LdpApiGateWay,代码行数:8,代码来源:Lock.cpp


示例19: Pthread_mutexattr_setpshared

void Pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int flag)
{
	int n;
	if ((n = pthread_mutexattr_setpshared(attr, flag)) != 0)
		err_sys("pthread_metxattr_setpshared error");

	return;
}
开发者ID:izerocrazy,项目名称:socket,代码行数:8,代码来源:base.cpp


示例20: main

int main(int argc, char *argv[]) {
	printf("Server started \n");

	/* Create shared memory */
	int file_descriptor;
	file_descriptor = shm_open("/sharedpid", O_RDWR | O_CREAT, S_IRWXU);

	/* Resize shared memory */
	ftruncate(file_descriptor, sizeof(pid_data));

	/* Map shared memory into address space */
	void* shared_memory;
	shared_memory = mmap(0, sizeof(pid_data), PROT_READ | PROT_WRITE, MAP_SHARED, file_descriptor, 0);

	/* Create struct */
	pid_data data;
	data.pid = getpid();

	/* Create Mutex */
	pthread_mutex_t lock = ((pid_data*)shared_memory)->pid_mutex;
	pthread_mutexattr_t myattr;
	pthread_mutexattr_init(&myattr);
	pthread_mutexattr_setpshared(&myattr, PTHREAD_PROCESS_SHARED);
	pthread_mutex_init(&lock, &myattr );

	/* Set pid in shared memory */
	pthread_mutex_lock(&lock);
	*((pid_data*)shared_memory) = data;
	pthread_mutex_unlock(&lock);
	printf("Pid stored in shared memory \n");


	/* Create channel */
	int ch_id = ChannelCreate(0);
	printf("Channel %d created \n", ch_id);
	int rcv_id;
	int buffer_recv;
	int buffer_resp;
	struct _msg_info msg_info;

	/* Set priority */
	set_priority(SERVER_PRIORITY);

	/* Listen for client */
	printf("Server listening... \n");
	while(1){
		printf("Server priority: %d\n", get_priority());
		rcv_id = MsgReceive(ch_id, &buffer_recv, sizeof(buffer_recv), &msg_info);
		buffer_resp = buffer_recv + 1;
		printf("Received: %d, Responding: %d \n", buffer_recv, buffer_resp);

		if(MsgReply(rcv_id, EOK, &buffer_resp, sizeof(buffer_resp)) != EOK){
			printf("ERROR: failed to reply to message: %d \n", buffer_recv);
		}
	}

	return EXIT_SUCCESS;
}
开发者ID:magnealvnor,项目名称:Sanntidssystemer,代码行数:58,代码来源:QNX_server.c



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ pthread_mutexattr_settype函数代码示例发布时间:2022-05-30
下一篇:
C++ pthread_mutexattr_setprotocol函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap