pthread_create暂停 Linux线程-互斥锁pthread_mutex_t (转载)(2)

电脑杂谈  发布时间:2018-02-09 13:20:36  来源:网络整理

int pthread_mutex_lock(pthread_mutex_t *mutex)

int pthread_mutex_unlock(pthread_mutex_t *mutex)

int pthread_mutex_trylock(pthread_mutex_t *mutex)

pthread_mutex_trylock()语义与pthread_mutex_lock()类似,不同的是在锁已经被占据时返回EBUSY而不是挂起等待

五,锁的使用

#include??<pthread.h>????

#include??<stdio.h>????

????

pthread_mutex_t??mutex??;????

void??*print_msg(void??*arg){????

????????????????int??i=0;????

????????????????pthread_mutex_lock(&mutex);????

pthread_create 参数_pthread_create暂停_pthreadcreate用法

????????????????for(i=0;i<15;i++){????

????????????????????????????????printf("output??:??%d\n",i);????

????????????????????????????????usleep(100);????

????????????????}????

????????????????pthread_mutex_unlock(&mutex);????

}????

int??main(int??argc,char**??argv){????

????????????????pthread_t??id1;????

????????????????pthread_t??id2;????

????????????????pthread_mutex_init(&mutex,NULL);????

????????????????pthread_create(&id1,NULL,print_msg,NULL);????

????????????????pthread_create(&id2,NULL,print_msg,NULL);????

????????????????pthread_join(id1,NULL);????

????????????????pthread_join(id2,NULL);????

????????????????pthread_mutex_destroy(&mutex);????

????????????????return??1;????

}????

将会一个线程一个线程的执行


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-68864-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...