#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\n");
pthread_mutex_lock(&lock);
printf_s("input: \n");
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:\n");
pthread_cond_signal(&empty);
}
}
出現問題:錯誤 1 error LNK2019: 無法解析的外部符號 "void * __cdecl put_buf(void *)" (?put_buf@@YAPAXPAX@Z),該符號在函數 _main 中被引用 c:\Users\陌桑時代shine\documents\visual studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\源.obj ConsoleApplication2
錯誤 2 error LNK2019: 無法解析的外部符號 "void * __cdecl get_buf(void *)" (?get_buf@@YAPAXPAX@Z),該符號在函數 _main 中被引用 c:\Users\陌桑時代shine\documents\visual studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\源.obj ConsoleApplication2
額,你要不就把函數寫在main函數前面,要不就寫上該函數的聲明!二選其一呀