您的位置:首页 > 运维架构 > Linux

Linux centOS c语言 多线程编程学习笔记

2016-04-08 22:04 543 查看
开始学习linux下c语言多线程编程,学习一位前辈的博客内容(http://blog.csdn.net/huqinwei987/article/details/7526667#comments),如下:

/*Hello,world--Single Thread*/
//记得-lpthread链接libpthread.a
#include<stdio.h>
#include<pthread.h>
#define NUM 6
int main(){
void print_msg(void*);

pthread_t t1, t2;
pthread_create(&t1, NULL, print_msg, (void*)"hello,");
pthread_create(&t2, NULL, print_msg, (void*)"world!\n");

pthread_join(t1, NULL);
pthread_join(t2, NULL);
}
void print_msg(void* m){
char *cp = (char*)m;//有个转换过程
int i;
for(i = 0 ; i < NUM ; i++){//隔一秒打印
printf("%s", m);
fflush(stdout);//清空
sleep(1);//睡眠一秒
}
}
/*
* 函数原型:
* int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*func)(void*),void *arg);
* 参数: thread 指向pthread_t类型变量的指针
* attr 指向pthread_attr_t类型变量的指针,或者为NULL
* func 指向新线程所运行函数的指针
* arg 传递给func的参数
* 返回值 0 成功返回
* errcode 错误
* 我们可以使用函数pthread_join等待某进程结束。
* 函数原型:int pthread_join(pthread_t thread,void ** retval);
* 参数: thread 所等待的进程
* retval 指向某存储线程返回值的变量
* 返回值: 0 成功返回
* errorcode 错误
* 以上两个函数都包含在头文件pthread.h中。
*/


敲入发现运行有报错

note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’

看的似懂非懂

直接复制百度一下

找到一个不错的答案
http://stackoverflow.com/questions/28018045/error-in-pointer-multithread-expected-void-void-but-argument-is-of/28018114
看完修改代码如下:



修改处用如图所标,运行成功

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: