0
我试图让一个程序的意义:未定义的引用在pthread_create
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int main(int argc, char** argv) {
volatile double fShared = 0.0;
// create pthread structures - one for each thread
pthread_t t0;
pthread_t t1;
//Arbitrary pointer variables - void* is a pointer to anything
void *thread_res0;
void *thread_res1;
int res0,res1;
//create a new thread AND run each function
// we pass the ADDRESS of the thread structure as the first argument
// we pass a function name as the third argument - this is known as a FUNCTION pointer
// you might want to look at the man pages for pthread_create
res0 = pthread_create(&t0, NULL, do_this, (void*)"");
res1 = pthread_create(&t1, NULL, do_that, (void*)"");
// Two threads are now running independently of each other and this main function
// the function main is said to run in the main thread, so we are actually running
// The main code can now continue while the threads run - so we can simply block
res0 = pthread_join(t0, &thread_res0);
res1 = pthread_join(t1, &thread_res1);
printf ("\n\nFinal result is fShared = %f\n",fShared);
return (EXIT_SUCCESS);
}
应当指出的是,do_this和do_that是先前在程序中创建功能。我收到以下错误:
[email protected]:~/Programs$ Homework2OS.c
/tmp/cceiRtg8.O: in function 'main'
Undefined reference to 'pthread_create'
Undefined reference to 'pthread_create'
Undefined reference to 'pthread_join'
Undefined reference to 'pthread_join'
我们得到的这段代码来解决。我在其他地方找到了pthread_create构造函数的格式。我是否需要在主体之上实际定义它?我的印象是与图书馆一起输入的。
我也冒昧猜测它与第四个参数有关吗?我知道第一个参数是线程的位置(上面定义),第二个参数是NULLed,第三个参数是函数调用,但我不太明白第四个参数应该是什么。
怎么了?
显示你的编译命令,和你的'#include'指令。阅读[pthread教程](https://computing.llnl.gov/tutorials/pthreads/)。你的操作系统是什么?阅读[pthread_create(3)](http://man7.org/linux/man-pages/man3/pthread_create.3.html) –
将'-lpthread'选项传递给你的编译器 – StoryTeller
其实你应该通过'-pthread'到你的''gcc'编译器,然后'-lpthread'库链接 –