本文整理汇总了C++中pthread_rwlock_init函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_rwlock_init函数的具体用法?C++ pthread_rwlock_init怎么用?C++ pthread_rwlock_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_rwlock_init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
void run() {
/**
* note: this test will deadlock if the code breaks
*/
#if defined(__linux__) || defined(__APPLE__)
// create
pthread_rwlock_t lk;
verify(pthread_rwlock_init(&lk, 0) == 0);
// read lock
verify(pthread_rwlock_rdlock(&lk) == 0);
AtomicUInt32 x1(0);
stdx::thread t1(stdx::bind(worker1, &lk, &x1));
while (!x1.load())
;
verify(x1.load() == 1);
sleepmillis(500);
verify(x1.load() == 1);
AtomicUInt32 x2(0);
stdx::thread t2(stdx::bind(worker2, &lk, &x2));
t2.join();
verify(x2.load() == 1);
pthread_rwlock_unlock(&lk);
for (int i = 0; i < 2000; i++) {
if (x1.load() == 2)
break;
sleepmillis(1);
}
verify(x1.load() == 2);
t1.join();
#endif
}
示例2: SpriteList
Region::Region(Palette* p, int i) {
spritelist = new SpriteList();
id = i;
name = "";
SetTypeAndSid(UNKNOWN, 0, 0);
_loop = NULL;
_disableNotes = false;
palette = p;
_lastScheduled = -1;
_chording = false;
_looping = DEFAULT_LOOPING;
NosuchLockInit(&_region_mutex,"region");
// spritelist_rwlock = PTHREAD_RWLOCK_INITIALIZER;
cursorlist_rwlock = PTHREAD_RWLOCK_INITIALIZER;
// int rc1 = pthread_rwlock_init(&spritelist_rwlock, NULL);
int rc = pthread_rwlock_init(&cursorlist_rwlock, NULL);
if ( rc ) {
NosuchDebug("Failure on pthread_rwlock_init!? rc=%d",rc);
}
_latestNoteTime = 0;
x_min = 0.00f;
y_min = 0.00f;
x_max = 1.0f;
y_max = 1.0f;
_channel = -1;
PaletteHost* ph = p->paletteHost();
_graphicBehaviour = ph->makeGraphicBehaviour(this);
_musicBehaviour = ph->makeMusicBehaviour(this);
_noteBehaviour = new NoteBehaviourDefault(this);
initParams();
}
示例3: TEST
TEST(sfrlock, uncontended_write_cost) {
double t;
double r;
pthread_mutex_t mutex;
pthread_rwlock_t rwlock;
sfrlock_t sfrlock;
sfrlock_init(&sfrlock);
pthread_rwlock_init(&rwlock, nullptr);
pthread_mutex_init(&mutex, nullptr);
r = measure_time([&] () {
for (unsigned cnt = repeat; cnt; cnt--) {
sfrlock_wrlock(&sfrlock);
sfrlock_wrunlock(&sfrlock);
}
});
printf("sfrlock_t time: %lf ms\n", r / 1e6);
t = measure_time([&] () {
for (unsigned cnt = repeat; cnt; cnt--) {
pthread_rwlock_wrlock(&rwlock);
pthread_rwlock_unlock(&rwlock);
}
});
printf("pthread_rwlock_t time: %lf ms (%+.2lf%%)\n", t / 1e6,
-(1 - (t / r)) * 100);
t = measure_time([&] () {
for (unsigned cnt = repeat; cnt; cnt--) {
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
}
});
printf("pthread_mutex_t time: %lf ms (%+.2lf%%)\n", t / 1e6,
-(1 - (t / r)) * 100);
pthread_rwlock_destroy(&rwlock);
pthread_mutex_destroy(&mutex);
}
示例4: _magic
ESFReadWriteLock::ESFReadWriteLock() :
_magic(0) {
#ifdef HAVE_PTHREAD_RWLOCK_INIT
if (0 == pthread_rwlock_init(&_lock, 0)) {
_magic = ESF_MAGIC;
}
#elif defined HAVE_PTHREAD_MUTEX_INIT && defined HAVE_PTHREAD_COND_INIT && \
defined HAVE_PTHREAD_MUTEX_DESTROY && defined HAVE_PTHREAD_COND_DESTROY
if ( 0 != pthread_mutex_init( &_lock._mutex, 0 ) )
{
return;
}
if ( 0 != pthread_cond_init( &_lock._readSignal, 0 ) )
{
pthread_mutex_destroy( &_lock._mutex );
return;
}
if ( 0 != pthread_cond_init( &_lock._writeSignal, 0 ) )
{
pthread_mutex_destroy( &_lock._mutex );
pthread_cond_destroy( &_lock._readSignal );
return;
}
_lock._readersActive = 0;
_lock._readersWaiting = 0;
_lock._writersActive = 0;
_lock._writersWaiting = 0;
_magic = ESF_MAGIC;
#else
#error "Platform has no rw lock initializer"
#endif
}
示例5: dt_control_init
void dt_control_init(dt_control_t *s)
{
memset(s->vimkey, 0, sizeof(s->vimkey));
s->vimkey_cnt = 0;
// same thread as init
s->gui_thread = pthread_self();
// initialize static mutex
dt_pthread_mutex_init(&_control_gdk_lock_threads_mutex, NULL);
// s->last_expose_time = dt_get_wtime();
s->key_accelerators_on = 1;
s->log_pos = s->log_ack = 0;
s->log_busy = 0;
s->log_message_timeout_id = 0;
dt_pthread_mutex_init(&(s->log_mutex), NULL);
s->progress = 200.0f;
dt_conf_set_int("ui_last/view", DT_MODE_NONE);
pthread_cond_init(&s->cond, NULL);
dt_pthread_mutex_init(&s->cond_mutex, NULL);
dt_pthread_mutex_init(&s->queue_mutex, NULL);
dt_pthread_mutex_init(&s->run_mutex, NULL);
pthread_rwlock_init(&s->xprofile_lock, NULL);
dt_pthread_mutex_init(&(s->global_mutex), NULL);
dt_pthread_mutex_init(&(s->progress_system.mutex), NULL);
// start threads
dt_control_jobs_init(s);
s->button_down = 0;
s->button_down_which = 0;
s->mouse_over_id = -1;
s->dev_closeup = 0;
s->dev_zoom_x = 0;
s->dev_zoom_y = 0;
s->dev_zoom = DT_ZOOM_FIT;
}
示例6: dnscacheInit
/* init function (must be called once) */
rsRetVal
dnscacheInit(void)
{
DEFiRet;
if((dnsCache.ht = create_hashtable(100, hash_from_key_fn, key_equals_fn,
(void(*)(void*))entryDestruct)) == NULL) {
DBGPRINTF("dnscache: error creating hash table!\n");
ABORT_FINALIZE(RS_RET_ERR); // TODO: make this degrade, but run!
}
dnsCache.nEntries = 0;
pthread_rwlock_init(&dnsCache.rwlock, NULL);
CHKiRet(objGetObjInterface(&obj)); /* this provides the root pointer for all other queries */
CHKiRet(objUse(glbl, CORE_COMPONENT));
CHKiRet(objUse(errmsg, CORE_COMPONENT));
CHKiRet(objUse(prop, CORE_COMPONENT));
prop.Construct(&staticErrValue);
prop.SetString(staticErrValue, (uchar*)"???", 3);
prop.ConstructFinalize(staticErrValue);
finalize_it:
RETiRet;
}
示例7: debug
DICTIONARY *dictionary_create(uint32_t flags) {
debug(D_DICTIONARY, "Creating dictionary.");
DICTIONARY *dict = calloc(1, sizeof(DICTIONARY));
if(unlikely(!dict)) fatal("Cannot allocate DICTIONARY");
if(flags & DICTIONARY_FLAG_WITH_STATISTICS) {
dict->stats = calloc(1, sizeof(struct dictionary_stats));
if(!dict->stats) fatal("Cannot allocate statistics for DICTIONARY");
}
if(!(flags & DICTIONARY_FLAG_SINGLE_THREADED)) {
dict->rwlock = calloc(1, sizeof(pthread_rwlock_t));
if(!dict->rwlock) fatal("Cannot allocate pthread_rwlock_t for DICTIONARY");
pthread_rwlock_init(dict->rwlock, NULL);
}
avl_init(&dict->values_index, name_value_compare);
dict->flags = flags;
return dict;
}
示例8: main
int main(int argc, char *argv[])
{
L = atol(argv[1]);
K = atol(argv[2]);
M = atol(argv[3]);
if (signal(SIGINT, exit_server) == SIG_ERR)
syserr("Error in signal (SIGINT)");
init_queues();
int thr_err;
pthread_t thread_id;
make_attr_detached();
if (thr_err = pthread_rwlock_init(&rwlock, NULL))
syserr_ext(thr_err, "Error in function pthread_rwlock_init");
Mesg mesg;
int bytes_rcvd;
while (1)
{
if ((bytes_rcvd = msgrcv(msg_rcv_id, &mesg, MAX_BUFF, 0, 0)) <= 0)
syserr("Error in msgrcv (receiving type(pid))");
mesg.mesg_data[bytes_rcvd] = '\0';
if (mesg.mesg_type == READ_TYPE_KOM)
{
if ((thr_err = pthread_create(&thread_id, &attr, serve_committee, &mesg.mesg_data)) != 0)
syserr_ext(thr_err, "Error in pthread_create (for serve_committee)");
}
else
{
if ((thr_err = pthread_create(&thread_id, &attr, serve_report, &mesg.mesg_data)) != 0)
syserr_ext(thr_err, "Error in pthread_create (for serve_report)");
}
}
exit_server(0);
}
示例9: init_read_write_lock
static void
init_read_write_lock(pthread_rwlock_t* lock)
{
pthread_rwlockattr_t* pattr;
#if defined(YOG_HAVE_PTHREAD_RWLOCKATTR_INIT)
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
# if defined(YOG_HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP)
pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
# endif
pattr = &attr;
#else
pattr = NULL;
#endif
int err;
if ((err = pthread_rwlock_init(lock, pattr)) != 0) {
YOG_BUG(NULL, "pthread_rwlock_init failed: %s", strerror(err));
}
#if defined(YOG_HAVE_PTHREAD_RWLOCKATTR_INIT) && defined(YOG_HAVE_PTHREAD_RWLOCKATTR_DESTROY)
pthread_rwlockattr_destroy(&attr);
#endif
}
示例10: diskfs_user_make_node
/* The user must define this function if she wants to use the node
cache. Create and initialize a node. */
error_t
diskfs_user_make_node (struct node **npp, struct lookup_context *ctx)
{
struct node *np;
struct disknode *dn;
/* Create the new node. */
np = diskfs_make_node_alloc (sizeof *dn);
if (np == NULL)
return ENOMEM;
/* Format specific data for the new node. */
dn = diskfs_node_disknode (np);
dn->dirents = 0;
dn->dir_idx = 0;
dn->pager = 0;
pthread_rwlock_init (&dn->alloc_lock, NULL);
pokel_init (&dn->indir_pokel, diskfs_disk_pager, disk_cache);
*npp = np;
return 0;
}
示例11: main
int main(int argc, char** argv)
{
pthread_t thread1;
pthread_t thread2;
#if 0
VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DRD_TRACE_ADDR,
&s_racy, 0, 0, 0, 0, 0);
#endif
pthread_rwlock_init(&s_rwlock, 0);
pthread_create(&thread1, 0, thread_func, 0);
pthread_create(&thread2, 0, thread_func, 0);
pthread_join(thread1, 0);
pthread_join(thread2, 0);
pthread_rwlock_destroy(&s_rwlock);
fprintf(stderr, "Result: %d\n", s_racy);
return 0;
}
示例12: main
int main(int argc, char **argv)
{
int retval, i;
pthread_t writer_id, reader_id;
pthread_attr_t attr;
int nreadercount = 1, nwritercount = 1;
if (argc != 2) {
fprintf(stderr, "usage, <%s threadcount>", argv[0]);
return -1;
}
retval = pthread_rwlock_init(&rwlock, NULL);
if (retval) {
fprintf(stderr, "init lock failed\n");
return retval;
}
pthread_attr_init(&attr);
//pthread_attr_setdetachstate用来设置线程的分离状态
//也就是说一个线程怎么样终止自己,状态设置为PTHREAD_CREATE_DETACHED
//表示以分离状态启动线程
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
//分别在main函数中对读出者和写入者加锁,得到的处理结果是不一样的
pthread_rwlock_wrlock(&rwlock);
// pthread_rwlock_rdlock(&rwlock);
for (i = 0; i < atoi(argv[1]); i++) {
if (random() % 2) {
pthread_create(&reader_id, &attr, readers, (void *)nreadercount);
printf("create reader %d\n", nreadercount++);
} else {
pthread_create(&writer_id, &attr, writers, (void *)nwritercount);
printf("create writer %d\n", nwritercount++);
}
}
pthread_rwlock_unlock(&rwlock);
sleep(20);//sleep是为了等待另外的线程的执行
return 0;
}
示例13: init_mapping
int init_mapping()
{
int i;
for (i = 0; i < MAP_TABLE_SIZE; i++) {
memset(&(ip_map_table[i]), 0, sizeof(struct ip_map_table_node));
if (pthread_rwlock_init(&(ip_map_table[i].rwlock), NULL) != 0) {
fprintf(stderr, "Error initialize mutex for map-table\n");
return(1);
}
}
for (i = 0; i < HASH_TABLE_SIZE; i++) {
memset(&(hash_table[i]), 0, sizeof(struct hash_table_array_node));
if (pthread_mutex_init(&(hash_table[i].mutex), NULL) != 0) {
fprintf(stderr, "Error initialize mutex for hash-table\n");
return(1);
}
}
for (i = 0; i < VSERVER_MAXSIZE; i++) {
memset(&(vserver_list[i]), 0, sizeof(struct vserver_list_node));
}
return(0);
}
示例14: fs_file_handle_create
// create a file handle from an fs_entry
struct fs_file_handle* fs_file_handle_create( struct fs_core* core, struct fs_entry* ent, char const* opened_path, uint64_t parent_id, char const* parent_name ) {
struct fs_file_handle* fh = SG_CALLOC( struct fs_file_handle, 1 );
fh->flags = 0;
fh->open_count = 0;
fh->fent = ent;
fh->volume = ent->volume;
fh->file_id = ent->file_id;
fh->path = strdup( opened_path );
fh->parent_name = strdup( parent_name );
fh->parent_id = parent_id;
fh->transfer_timeout_ms = (core->conf->transfer_timeout) * 1000L;
fh->dirty = false;
uint64_t gateway_type = ms_client_get_gateway_type( core->ms, ent->coordinator );
if( gateway_type == SYNDICATE_AG ) {
fh->is_AG = true;
}
pthread_rwlock_init( &fh->lock, NULL );
return fh;
}
示例15: tpclog_init
/* Initialize TPCLog LOG to use the provided DIRNAME to store its associated
* entries. Sets LOG's NEXTID field based on the entries that currently exist
* in DIRNAME. */
int tpclog_init(tpclog_t *log, char *dirname) {
struct stat st;
unsigned long nextid = 0;
char filename[MAX_FILENAME];
if (stat(dirname, &st) == -1) {
if (mkdir(dirname, 0700) == -1)
return errno;
}
log->dirname = malloc(strlen(dirname) + 1);
if (!log->dirname)
fatal_malloc();
strcpy(log->dirname, dirname);
pthread_rwlock_init(&log->lock, NULL);
/* Iterate through entries to determine next available ID, since this log may
* be recovering from a crash. */
sprintf(filename, "%s/%lu%s", log->dirname, nextid++, TPCLOG_FILETYPE);
while (stat(filename, &st) != -1)
sprintf(filename, "%s/%lu%s", log->dirname, nextid++, TPCLOG_FILETYPE);
log->nextid = nextid - 1;
return 0;
}