?? thread error.txt
字號:
關(guān)于linux下多線程編程,小弟遇到了點麻煩,請各位指導(dǎo)一下,謝謝.
這是編譯時的錯誤信息:
[root@localhost thread]# g++ -o mutilthread main.cpp
main.cpp: In function `int main()':
main.cpp:14: error: invalid conversion from `void*' to `void*(*)(void*)'
main.cpp:14: error: initializing argument 3 of `int pthread_create(pthread_t*, const pthread_attr_t*, void*(*)(void*), void*)'
main.cpp:17: error: `exit' undeclared (first use this function)
main.cpp:17: error: (Each undeclared identifier is reported only once for each function it appears in.)
這是源代碼:main.cpp
#include <stdio.h>
#include <pthread.h>
void thread(void)
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread.\n");
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
for(i=0;i<3;i++)
printf("This is the main process.\n");
pthread_join(id,NULL);
return (0);
}
1。編碼問題
void thread(void)
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread.\n");
}
改為
void* thread(void)
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread.\n");
return NULL;
}
ret=pthread_create(&id,NULL,(void *) thread,NULL);
改為ret=pthread_create(&id,NULL, thread,NULL);
2。編譯問題
g++ -o mutilthread main.cpp
改為
g++ -o mutilthread main.cpp -lpthread
------------
錯了一個地方:pthread_create(&id,NULL,(void *) thread,NULL);
把,(void *) thread改成,(void *) &thread
一切ok
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -