关于线程同步例程中的一个问题,大家看看

antinet 2012-11-01 06:46:56
有一个线程同步的例子,程序是对的,这里只贴出看不明白的部分。
1. 开始的typedef void* (*fun)(void*);有什么用,这句话应该如何理解?
2. 使用的时候,func前后加了2个下划线,为什么?
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>

typedef void* (*fun)(void*); //** 这有什么用?如何理解?

int g_Flag=0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* thread1(void*);
void* thread2(void*);

/*
* when program is started, a single thread is created, called the initial thread or main thread.
* Additional threads are created by pthread_create.
* So we just need to create two thread in main().
*/

int main(int argc, char** argv)
{
printf("enter main\n");
pthread_t tid1, tid2;
int rc1=0, rc2=0;
rc2 = pthread_create(&tid2, NULL, thread2, NULL);
if(rc2 != 0)
printf("%s: %d\n",__func__, strerror(rc2)); //** 这里为什么__func__,前后都是2个下划线

rc1 = pthread_create(&tid1, NULL, thread1, &tid2);
if(rc1 != 0)
printf("%s: %d\n",__func__, strerror(rc1));

pthread_cond_wait(&cond, &mutex);
printf("leave main\n");
exit(0);
}
...全文
95 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
fdl19881 2012-11-01
  • 打赏
  • 举报
回复
c语言没学好 
typedef void* (*fun)(void*);//定义一个函数指针类型
__func__这是一个宏,默认定义的.用来表示当前所在函数名(字符串),如果此句输出,即为main:

详细解释看下面:
http://blog.csdn.net/dingyuanpu/article/details/5572362
http://blog.csdn.net/taric_ma/article/details/7517949

23,114

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧