问题描述
- windows下使用pthread.h库的问题
-
#include
#include
#include
#include
#include
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t full = PTHREAD_COND_INITIALIZER;
char buf[256];int main()
{
pthread_t t1,t2;
void * put_buf(void *);
void * get_buf(void *);
pthread_mutex_lock(&lock);
pthread_cond_signal(&empty);
pthread_create(&t1, NULL, put_buf, NULL);
pthread_create(&t2, NULL, get_buf, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
void *put_buf()
{
while (true)
{
pthread_cond_wait(&empty, &lock);
printf_s("empty flag is raised
");
pthread_mutex_lock(&lock);
printf_s("input:
");
gets_s(buf);
pthread_mutex_unlock(&lock);
pthread_cond_signal(&full);}
}
void *get_buf(){
Sleep(2);
while (true)
{
pthread_cond_wait(&full, &lock);
printf_s("full flag is raised");
pthread_mutex_lock(&lock);
printf_s("output:
");
pthread_cond_signal(&empty);
}
}出现问题:错误 1 error LNK2019: 无法解析的外部符号 "void * __cdecl put_buf(void *)" (?put_buf@@YAPAXPAX@Z),该符号在函数 _main 中被引用 c:Users陌桑时代shinedocumentsvisual studio 2013ProjectsConsoleApplication2ConsoleApplication2源.obj ConsoleApplication2
错误 2 error LNK2019: 无法解析的外部符号 "void * __cdecl get_buf(void *)" (?get_buf@@YAPAXPAX@Z),该符号在函数 _main 中被引用 c:Users陌桑时代shinedocumentsvisual studio 2013ProjectsConsoleApplication2ConsoleApplication2源.obj ConsoleApplication2
解决方案
额,你要不就把函数写在main函数前面,要不就写上该函数的声明!二选其一呀
解决方案二:
windows下使用pthread库
windows下使用pthread库
windows下使用pthread库