当前位置: 技术问答>linux和unix
线程锁函数pthread_cleanup_push()编译问题,急!
来源: 互联网 发布时间:2016-11-09
本文导语: void * child1(void *arg) { pthread_cleanup_push(pthread_mutex_unlock,&mutex); /* comment 1 */ while(1){ printf("thread 1 get running n"); printf("thread 1 pthread_mutex_lock returns %dn"...
void * child1(void *arg)
{
pthread_cleanup_push(pthread_mutex_unlock,&mutex); /* comment 1 */
while(1){
printf("thread 1 get running n");
printf("thread 1 pthread_mutex_lock returns %dn",
pthread_mutex_lock(&mutex));
pthread_cond_wait(&cond,&mutex);
printf("thread 1 condition appliedn");
pthread_mutex_unlock(&mutex);
sleep(5);
}
pthread_cleanup_pop(0); /* comment 2 */
}
如上参照网上的一段代码,在编译时,老是报:
invalid conversion from 'int (*)(pthread_mutex_t*)throw ()' to 'void (*)(void*)' 错误
看了下pthread_cleanup_push原型为:
void pthread_cleanup_push(void (*routine)(void*), void *arg)
而pthread_mutex_unlock原型为:
int pthread_mutex_unlock(pthread_mutex_t *mutex);
上述程序是在LINUX主机上编译的。
这种问题该如何解决啊,谢谢各位了!
{
pthread_cleanup_push(pthread_mutex_unlock,&mutex); /* comment 1 */
while(1){
printf("thread 1 get running n");
printf("thread 1 pthread_mutex_lock returns %dn",
pthread_mutex_lock(&mutex));
pthread_cond_wait(&cond,&mutex);
printf("thread 1 condition appliedn");
pthread_mutex_unlock(&mutex);
sleep(5);
}
pthread_cleanup_pop(0); /* comment 2 */
}
如上参照网上的一段代码,在编译时,老是报:
invalid conversion from 'int (*)(pthread_mutex_t*)throw ()' to 'void (*)(void*)' 错误
看了下pthread_cleanup_push原型为:
void pthread_cleanup_push(void (*routine)(void*), void *arg)
而pthread_mutex_unlock原型为:
int pthread_mutex_unlock(pthread_mutex_t *mutex);
上述程序是在LINUX主机上编译的。
这种问题该如何解决啊,谢谢各位了!
|
pthread_cleanup_push()的第一个参数要求是一个没有返回值的函数
而pthread_mutex_unlock是一个返回int 的函数
而pthread_mutex_unlock是一个返回int 的函数