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


C++ Pthread_mutex_lock函数代码示例

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


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

示例1: LOG_INFO_NOENDL

void DiskMgr::ReadSeg(int fileid, int segid, int diskid)
{
	LOG_INFO_NOENDL("fake waiting thread distribution: ");
	for (int i = 0; i < m_diskNumber; ++i) {
		LOG_NOTIME_NOENDL(m_diskInfo[i].waitingThread << "\t");
	}
	LOG_NOTIME_NOENDL(endl);
	Pthread_mutex_lock(&m_diskInfo[diskid].mutex);
	++m_diskInfo[diskid].waitingThread;
	while (m_diskInfo[diskid].readingThread >= m_curReadThreadNum) {
		pthread_cond_wait(&m_diskInfo[diskid].cond, &m_diskInfo[diskid].mutex);
	}
	++m_diskInfo[diskid].readingThread;
	--m_diskInfo[diskid].waitingThread;
	Pthread_mutex_unlock(&m_diskInfo[diskid].mutex);
	
	//usleep(200000);
	double temp = m_blockSize * 1.0 / m_diskBand;
	LOG_INFO("server fake read, sleep " << temp << "s");
	usleep((int)(temp * 1000000));
	
	Pthread_mutex_lock(&m_diskInfo[diskid].mutex);
	--m_diskInfo[diskid].readingThread;
	pthread_cond_broadcast(&m_diskInfo[diskid].cond);
	Pthread_mutex_unlock(&m_diskInfo[diskid].mutex);
	Pthread_mutex_lock(&m_mutex);
	++m_totalRead;
	Pthread_mutex_unlock(&m_mutex);
}
开发者ID:olyd,项目名称:st,代码行数:29,代码来源:diskmgr.cpp

示例2: high

/* @pid = medium priority process pid */
void high(pid_t pid)
{
	int status;
	set_rt_prio(0, prio_min+2, policy);

	/* Must come after raising the priority */
	Pthread_mutex_lock(statep->mutex);
		statep->high_started = 1;
	Pthread_mutex_unlock(statep->mutex);

	Pthread_mutex_lock(resource);
	Pthread_mutex_lock(statep->mutex);
		statep->high_owns_resource = 1;
		if (!statep->low_owns_resource || !statep->medium_started) {
			statep->inversion = 0;
		}
	Pthread_mutex_unlock(statep->mutex);
	Pthread_mutex_unlock(resource);
	kill(pid, SIGKILL);	/* kill the medium thread */
	waitpid(pid, &status, 0);

	Pthread_mutex_lock(statep->mutex);

	if (statep->inversion)
		printf("Successfully used priority inheritance to handle an inversion\n");
	else {
		printf("No inversion incurred\n");
	}
	Pthread_mutex_unlock(statep->mutex);
}
开发者ID:1023xp,项目名称:training,代码行数:31,代码来源:pip_stress.c

示例3: cerebrod_speaker_data_initialize

void
cerebrod_speaker_data_initialize(void)
{
#if !WITH_CEREBROD_NO_THREADS
    Pthread_mutex_lock(&speaker_data_init_lock);
#endif /* !WITH_CEREBROD_NO_THREADS */
    if (speaker_data_init)
        goto out;

#if !WITH_CEREBROD_NO_THREADS
    /*
     * Must lock in this initialization routine, b/c the update thread
     * in a metric module may call the update state function.
     */
    Pthread_mutex_lock(&metric_list_lock);
#endif /* !WITH_CEREBROD_NO_THREADS */

    metric_list = List_create((ListDelF)_destroy_speaker_metric_info);

    if (_setup_metric_modules() < 0)
        CEREBRO_EXIT(("_setup_metric_modules"));

#if !WITH_CEREBROD_NO_THREADS
    Pthread_mutex_unlock(&metric_list_lock);
#endif /* !WITH_CEREBROD_NO_THREADS */

    speaker_data_init++;
out:
#if !WITH_CEREBROD_NO_THREADS
    Pthread_mutex_unlock(&speaker_data_init_lock);
#endif /* !WITH_CEREBROD_NO_THREADS */
    ;                             /* in case !WITH_CEREBRO_NO_THREADS */
}
开发者ID:alepharchives,项目名称:cerebro,代码行数:33,代码来源:cerebrod_speaker_data.c

示例4: thread_loop

static void* thread_loop(void *arg) {
    threadpool_t *pool = (threadpool_t*)arg;
    task_t *t = NULL;
    struct timespec ts;
    struct timeval  tv;
    int ret;
    int tosignal;

    while (!pool->exit) {
        Pthread_mutex_lock(&pool->mutex);
        gettimeofday(&tv, NULL);
        ts.tv_sec = tv.tv_sec + POOL_MAX_IDLE;
        ts.tv_nsec = tv.tv_usec * 1000;

        while (pool->task_queue.len == 0) {
            ret = Pthread_cond_timedwait(&pool->cond, &pool->mutex, &ts);
            if (ret == 0) {
                if (pool->exit) {
                    goto EXIT;
                }
                break;
            } else if (ret == ETIMEDOUT) {
                goto EXIT;
            }
        }

        --pool->threads_idle;
        t = heap_remove(&pool->task_queue, 0);
        tosignal = (pool->task_queue.len == 0) ? 1 : 0;
        Pthread_mutex_unlock(&pool->mutex);

        if (tosignal) {
            Pthread_cond_broadcast(&pool->task_over_cond);
        }

        if (t) {
            t->func(t->arg);
            free(t);
        }

        Pthread_mutex_lock(&pool->mutex);
        ++pool->threads_idle;
        Pthread_mutex_unlock(&pool->mutex);
    }

    Pthread_mutex_lock(&pool->mutex);
EXIT:
    --pool->threads_idle;
    tosignal = --pool->threads_num ? 0 : 1;
    Pthread_mutex_unlock(&pool->mutex);
    if (tosignal) {
        Pthread_cond_broadcast(&pool->exit_cond);
    }
    return NULL;
}
开发者ID:flygoast,项目名称:filmond,代码行数:55,代码来源:threadpool.c

示例5: _cerebrod_listener_initialize

/*
 * _cerebrod_listener_initialize
 *
 * perform listener initialization
 */
static void
_cerebrod_listener_initialize(void)
{
  int i;

  Pthread_mutex_lock(&listener_init_lock);
  if (listener_init)
    goto out;

  Pthread_mutex_lock(&listener_fds_lock);
  for (i = 0; i < conf.listen_message_config_len; i++)
    {
      if ((listener_fds[i] = _listener_setup_socket(i)) < 0)
        CEREBROD_EXIT(("listener fd setup failed"));
    }
  Pthread_mutex_unlock(&listener_fds_lock);

  if (!(clusterlist_handle = clusterlist_module_load()))
    CEREBROD_EXIT(("clusterlist_module_load"));
  
  if ((found_clusterlist_module = clusterlist_module_found(clusterlist_handle)) < 0)
    CEREBROD_EXIT(("clusterlist_module_found"));

  if (found_clusterlist_module)
    {
      if (clusterlist_module_setup(clusterlist_handle) < 0)
        CEREBROD_EXIT(("clusterlist_module_setup"));

      if (conf.debug && conf.listen_debug)
        {
          fprintf(stderr, "**************************************\n");
          fprintf(stderr, "* Cerebro Clusterlist\n");
          fprintf(stderr, "* -----------------------\n");
          fprintf(stderr, "* Using Clusterlist: %s\n", 
                  clusterlist_module_name(clusterlist_handle));
          fprintf(stderr, "**************************************\n");
        }
    }

  cerebrod_listener_data_initialize();

  for (i = 0; i < conf.forward_message_config_len; i++)
    {
      /* if the forward destination is local to the machine, don't forward */
      if (conf.forward_message_config[i].ip_is_local)
        continue;
      if (_forwarding_setup(i) < 0)
        CEREBROD_EXIT(("forwarding setup failed"));
    }

  listener_init++;
  Pthread_cond_signal(&listener_init_cond);
 out:
  Pthread_mutex_unlock(&listener_init_lock);
}
开发者ID:BenCasses,项目名称:cerebro,代码行数:60,代码来源:cerebrod_listener.c

示例6: add_to_list

/* add_to_list takes a cell and adds that cell the
 * the cache list. It locks the cache for writing so
 * that other threads cannot modify it. */
void add_to_list(struct cache_cell *cell)
{
	/* locks the cache for writing */
	Pthread_rwlock_wrlock(&cache_lock);
	/* if there is enough space in the cache,
	 * no eviction is needed. */
	if (cache_size + cell->size <= MAX_CACHE_SIZE)
	{
		cell->next = head;
		if (head != NULL)
			head->previous = cell;
		head = cell;
		cache_size += cell->size;
		cell->last_use = cache_time;
		
		Pthread_mutex_lock(&time_mutex);
		cache_time++;
		Pthread_mutex_unlock(&time_mutex);
	}
	/* if there is not enough space in the cache,
	 * eviction is needed. */
	else
	{
		struct cache_cell *tmp_cell, *ptr;
		int tmp_last_use;
		
		/* remove elements from cache so that there is enough
		 * space in the cache. */
		while (!(cache_size + cell->size <= MAX_CACHE_SIZE))
		{
			tmp_last_use = cache_time + 1;
			for (ptr = head; ptr != NULL; ptr = ptr->next)
				if (ptr->last_use < tmp_last_use)
				{
					tmp_last_use = ptr->last_use;
					tmp_cell = ptr;
				}
			remove_from_list(tmp_cell);
		}
		
		/* add cell to cache */
		cell->next = head;
		if (head != NULL)
			head->previous = cell;
		head = cell;
		cache_size += cell->size;
		cell->last_use = cache_time;
		
		Pthread_mutex_lock(&time_mutex);
		cache_time++;
		Pthread_mutex_unlock(&time_mutex);
	}
	Pthread_rwlock_unlock(&cache_lock);
	return;
}
开发者ID:samzcmu,项目名称:proxy,代码行数:58,代码来源:proxy.c

示例7: Producer

void *
Producer(void *v)
{
	struct producer *p = (struct producer *)v;
	struct consumer *c;
	struct packet *pa, *old;
	unsigned seed = p->seed;
	int n, k, cons;
	unsigned sum = 0;
	int waits = 0;
	int more = 1;
	int item_cnt = 0;

	cons = seed % num_consumers;
	for (n = 0; more; ++n) {

		/* Hole Packet aus lokaler emptyPacketQueue */
		Pthread_mutex_lock(&p->queue_mutex);
		while (!p->emptyPacketQueue) {
			++waits;
			Pthread_cond_wait(&p->empty_cond, &p->queue_mutex);
		}
		pa = p->emptyPacketQueue;
		p->emptyPacketQueue = pa->next;
		Pthread_mutex_unlock(&p->queue_mutex);

		/* Fuelle Packet */
		for (k = 0; k < buflen; ++k) {
			pa->buf[k] = ProduceItem(&seed, &sum);
			if (++item_cnt == num_items) {
				more = 0;
				++k;
				break;
			}
		}
		pa->len = k;

		/* Versende Packet an Consumer cons */
		c = consumers + cons;
		Pthread_mutex_lock(&c->queue_mutex);
		old = pa->next = c->fullPacketQueue;
		c->fullPacketQueue = pa;
		Pthread_mutex_unlock(&c->queue_mutex);
		if (!old)
			Pthread_cond_broadcast(&c->empty_cond);

		if (++cons == num_consumers)
			cons = 0;
	}

	p->sum = sum;
	printf("Producer %d exit, %d waits, %d packets\n", p->no, waits, n);
	return NULL;
}
开发者ID:jyting,项目名称:CPagerank,代码行数:54,代码来源:prod_cons2.c

示例8: thread_main_dns

void *
thread_main_dns(void *arg)
{
	int dnsrequest(const char *name, uint32 *ipaddr);
	int connfd, *number, count, total;
	char  *domain ;
	uint32 *ipptr;
	pthread_mutex_t *mutex;
	printf("dns thread %d starting \n", (int)arg );

	for(;;){

		Pthread_mutex_lock(&dns_array_mutex);

		while(iget == iput)
			Pthread_cond_wait(&dns_array_cond,
					&dns_array_mutex);
		
		printf("thread %d  working \n", pthread_self());

		connfd	= dns_array[iget].sockfd;
		domain  = dns_array[iget].domain;
		number	= dns_array[iget].number;
 		count	= dns_array[iget].count;
		total	= dns_array[iget].total;
		ipptr	= dns_array[iget].ipptr;
		mutex	= dns_array[iget].mutex;
		
		if(++iget == ARRAYSIZE)
			iget = 0;		

		Pthread_mutex_unlock(&dns_array_mutex);

		//do our job 
		if(dnsrequest(domain, ipptr + count) != 0) 
			inet_pton(AF_INET, "127.0.0.1", ipptr+count);
		dc_free(domain);	//dc_free the memory of domain
		Pthread_mutex_lock(mutex);
		(*number)--;
		Pthread_mutex_unlock(mutex);
		if((*number) == 0){
			write(connfd, ipptr, total*sizeof(uint32));
			printf("sended\n");
	//		close(connfd);	//todo
			dc_free(ipptr);
			dc_free(number);
			dc_free(mutex);
		} 


	}

}
开发者ID:gmsh,项目名称:dnscache,代码行数:53,代码来源:dns_thread_make.c

示例9: miss_handler

/* miss_handler handles miss case. It passes client's request
 * to server and passes server's response to client. If the 
 * response satisfies size requirements, store the response 
 * into cache. */
void miss_handler(int clientfd, struct client_request *request)
{
	int serverfd, length, response_size;
	char buf[MAXBUF], object_buf[MAX_OBJECT_SIZE];
	struct cache_cell *cell;
	rio_t rio_server;
	
	response_size = length = 0;
	
	/* acts as a client and writes request to server */
	Pthread_mutex_lock(&open_mutex);
	serverfd = open_serverfd_h(request, clientfd);
	Pthread_mutex_unlock(&open_mutex);
	rio_readinitb(&rio_server, serverfd);
	
	if (rio_writen(serverfd, request->request, request->request_length) 
					!= request->request_length)
	{
		write(2, "write error\n", strlen("write error\n"));
		close_connection(request, clientfd, serverfd);
	}
	
	/* passes server's response to client */
	while (1)
	{
		if ((length = rio_readnb(&rio_server, buf, MAXBUF)) < 0)
			close_connection(request, clientfd, serverfd);
		if (response_size + length <= MAX_OBJECT_SIZE)
			memcpy(object_buf + response_size, buf, length);
		response_size += length;
		if (rio_writen(clientfd, buf, length) < length)
			break;
		if (length != MAXBUF)
			break;
	}
	
	/* if response satisfies size requirement, store the response 
	 * into cache */
	if (response_size <= MAX_OBJECT_SIZE)
	{
		/* need a mutex to prevent inserting the same cell twice 
		 * into cache in race condition */
		Pthread_mutex_lock(&dup_mutex);
		if (search_cell_variant(request->request_line) == 0)
		{
			cell = allocate_cell();
			set_cell(cell, request->request_line, object_buf, response_size);
			add_to_list(cell);
		}
		Pthread_mutex_unlock(&dup_mutex);
	}
	close_connection(request, clientfd, serverfd);
}
开发者ID:samzcmu,项目名称:proxy,代码行数:57,代码来源:proxy.c

示例10: cerebrod_monitor_modules_update

/* 
 * cerebrod_monitor_modules_update
 *
 * Send metric data to the appropriate monitor modules, if necessary.
 * The struct cerebrod_node_data lock should already be locked.
 */
void
cerebrod_monitor_modules_update(const char *nodename,
                                struct cerebrod_node_data *nd,
                                const char *metric_name,
                                struct cerebrod_message_metric *mm)
{
  struct cerebrod_monitor_module_info *monitor_module;
  struct cerebrod_monitor_module_list *ml;
#if CEREBRO_DEBUG
  int rv;
#endif /* CEREBRO_DEBUG */
  
  assert(nodename && nd && metric_name && mm);

  if (!monitor_index)
    return;

#if CEREBRO_DEBUG
  /* Should be called with lock already set */
  rv = Pthread_mutex_trylock(&nd->node_data_lock);
  if (rv != EBUSY)
    CEREBROD_EXIT(("mutex not locked: rv=%d", rv));
#endif /* CEREBRO_DEBUG */

  if ((ml = Hash_find(monitor_index, metric_name)))
    {
      ListIterator itr = NULL;

      Pthread_mutex_lock(&(ml->monitor_list_lock));
      itr = List_iterator_create(ml->monitor_list);
      Pthread_mutex_unlock(&(ml->monitor_list_lock));

      while ((monitor_module = list_next(itr)))
	{
	  Pthread_mutex_lock(&monitor_module->monitor_lock);
	  monitor_module_metric_update(monitor_handle,
				       monitor_module->index,
				       nodename,
				       metric_name,
				       mm->metric_value_type,
				       mm->metric_value_len,
				       mm->metric_value);
	  Pthread_mutex_unlock(&monitor_module->monitor_lock);
	}

      Pthread_mutex_lock(&(ml->monitor_list_lock));
      List_iterator_destroy(itr);
      Pthread_mutex_unlock(&(ml->monitor_list_lock));
    }
}
开发者ID:BenCasses,项目名称:cerebro,代码行数:56,代码来源:cerebrod_monitor_update.c

示例11: free_cell

/* free_cell frees a cell and destroys the lock in that cell */
void free_cell(struct cache_cell *cell)
{
	if (cell->content != NULL)
	{
		Pthread_mutex_lock(&free_mutex);
		free(cell->content);
		Pthread_mutex_unlock(&free_mutex);
	}
	Pthread_rwlock_destroy(&(cell->lock));
	Pthread_mutex_lock(&free_mutex);
	free(cell);
	Pthread_mutex_unlock(&free_mutex);
	return;
}
开发者ID:samzcmu,项目名称:proxy,代码行数:15,代码来源:proxy.c

示例12: low

/* @pid = high priority process pid */
void low(pid_t pid)
{
	int status;
	Pthread_mutex_lock(resource);
		Pthread_mutex_lock(statep->mutex);
			statep->low_owns_resource = 1;
			if (statep->high_owns_resource ||
					statep->medium_started) {
				statep->inversion = 0;
			}
		Pthread_mutex_unlock(statep->mutex);
		usleep(500);
	Pthread_mutex_unlock(resource);
	waitpid(pid, &status, 0);
}
开发者ID:1023xp,项目名称:training,代码行数:16,代码来源:pip_stress.c

示例13: Producer

void *
Producer(void *v)
{
	struct thread_data *data = (struct thread_data *)v;
	unsigned seed = data->seed;
	int n;
	unsigned item, sum;
	int waits = 0;

	data->sum = 0;
	for (n = 0; n < num_items; ++n) {
		pthread_testcancel();

		sum = 0;
		item = ProduceItem(&seed, &sum);
	
		pthread_cleanup_push(pthread_mutex_unlock, &buf_mutex);

		Pthread_mutex_lock(&buf_mutex);
		while (buflen == maxbuflen) {
			++waits;
			Pthread_cond_wait(&full_cond, &buf_mutex);
		}
		buf[buflen++] = item;
		data->sum += sum;
		if (buflen == 1)
			Pthread_cond_broadcast(&empty_cond);

		pthread_cleanup_pop(1);
	}

	printf("Producer %d exit, %d waits\n", data->no, waits);
	return data;
}
开发者ID:jyting,项目名称:CPagerank,代码行数:34,代码来源:prod_cons_int.c

示例14: handler

void handler(void)
{
	Pthread_mutex_lock(&mp);
	cnt++;
	fprintf(stdout, "cnt[%d]\n", cnt);
	Pthread_mutex_unlock(&mp);
}
开发者ID:mallius,项目名称:POSIX,代码行数:7,代码来源:pthread_mutex_init.c

示例15: consume

void *
consume(void *arg)
{
	for ( ; ; ) {
		Pthread_mutex_lock(&nready.mutex);
		if (nready.nconsumer > 0) {
			Pthread_mutex_unlock(&nready.mutex);
			continue;
		} else if (nready.nget >= nitems) {
			Pthread_mutex_unlock(&nready.mutex);
			return(NULL);	
		} else {
			nready.nconsumer++;
			while (nready.nready == 0)
				Pthread_cond_wait(&nready.cond, &nready.mutex);
				nready.nready--;
				nready.nget++;
				nready.nconsumer--;
				Pthread_mutex_unlock(&nready.mutex);

				// if (buff[i] != i)
				// 	printf("buff[%d] = %d\n", i, buff[i]);
			
				*((int *) arg) += 1;
			}
	}
	
	return(NULL);
}
开发者ID:kissghosts,项目名称:uh-courses,代码行数:29,代码来源:prodcons6.c


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