pthread_cond_wait() & pthread_cond_signal() 调用顺序是什么?
What is the pthread_cond_wait() & pthread_cond_signal() calling sequence?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t lockid;
pthread_cond_t cvar = PTHREAD_COND_INITIALIZER;
char name[14];
void *entry()
{
pthread_mutex_lock(&lockid);
printf("Enter Name\n");
scanf("%s",name);
pthread_cond_signal(&cvar);
pthread_mutex_unlock(&lockid);
pthread_exit(NULL);
}
void *display()
{
pthread_mutex_lock(&lockid);
pthread_cond_wait(&cvar,&lockid);
printf("Name: %s\n",name);
pthread_mutex_unlock(&lockid);
pthread_exit(NULL);
}
int main()
{
pthread_t id1,id2;
if(pthread_mutex_init(&lockid,NULL)== 0){
pthread_create(&id1,NULL,&entry,NULL);
pthread_create(&id2,NULL,&display,NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
}
return 0;
}
我希望线程 id2 打印我在线程 id1 上输入的名称。
我知道 pthread_cond_wait()
不允许线程 id2
显示在线程 id1
上输入的名称,但是应该在 wait()
之前调用 signal()
对吗?
wait()
和signal()
方法的调用方式是否正确?我是 linux 编程的新手,请帮助我。
如果您有顺序任务,请不要使用线程:
#include <stdio.h>
char name[14];
int main() {
printf("Enter Name\n");
scanf("%s",name);
printf("Name: %s\n", name);
return 0
}
您可以使用 state
变量来确保 entry()
运行 在 display()
:
之前
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
pthread_mutex_t lockid;
pthread_cond_t cvar = PTHREAD_COND_INITIALIZER;
char name[14];
enum { START, ENTRY } state = START;
#define CHECK(e) { int error = (e); if(error) printf("%s:%d %s", __FILE__, __LINE__, strerror(error)); }
#define CHECK2(p, e) { if(p) CHECK(e); }
void *entry() {
CHECK(pthread_mutex_lock(&lockid));
printf("Enter Name\n");
CHECK2(scanf("%s", name) == EOF, errno);
state = ENTRY;
pthread_cond_signal(&cvar);
CHECK(pthread_mutex_unlock(&lockid));
return 0;
}
void *display() {
CHECK(pthread_mutex_lock(&lockid));
while(state != ENTRY) pthread_cond_wait(&cvar, &lockid);
printf("Name: %s\n", name);
CHECK(pthread_mutex_unlock(&lockid));
return 0;
}
int main() {
pthread_mutex_init(&lockid, NULL);
pthread_t id1, id2;
CHECK(pthread_create(&id1, NULL, &entry, NULL));
CHECK(pthread_create(&id2, NULL, &display, NULL));
CHECK(pthread_join(id1,NULL));
CHECK(pthread_join(id2,NULL));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t lockid;
pthread_cond_t cvar = PTHREAD_COND_INITIALIZER;
char name[14];
void *entry()
{
pthread_mutex_lock(&lockid);
printf("Enter Name\n");
scanf("%s",name);
pthread_cond_signal(&cvar);
pthread_mutex_unlock(&lockid);
pthread_exit(NULL);
}
void *display()
{
pthread_mutex_lock(&lockid);
pthread_cond_wait(&cvar,&lockid);
printf("Name: %s\n",name);
pthread_mutex_unlock(&lockid);
pthread_exit(NULL);
}
int main()
{
pthread_t id1,id2;
if(pthread_mutex_init(&lockid,NULL)== 0){
pthread_create(&id1,NULL,&entry,NULL);
pthread_create(&id2,NULL,&display,NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
}
return 0;
}
我希望线程 id2 打印我在线程 id1 上输入的名称。
我知道 pthread_cond_wait()
不允许线程 id2
显示在线程 id1
上输入的名称,但是应该在 wait()
之前调用 signal()
对吗?
wait()
和signal()
方法的调用方式是否正确?我是 linux 编程的新手,请帮助我。
如果您有顺序任务,请不要使用线程:
#include <stdio.h>
char name[14];
int main() {
printf("Enter Name\n");
scanf("%s",name);
printf("Name: %s\n", name);
return 0
}
您可以使用 state
变量来确保 entry()
运行 在 display()
:
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
pthread_mutex_t lockid;
pthread_cond_t cvar = PTHREAD_COND_INITIALIZER;
char name[14];
enum { START, ENTRY } state = START;
#define CHECK(e) { int error = (e); if(error) printf("%s:%d %s", __FILE__, __LINE__, strerror(error)); }
#define CHECK2(p, e) { if(p) CHECK(e); }
void *entry() {
CHECK(pthread_mutex_lock(&lockid));
printf("Enter Name\n");
CHECK2(scanf("%s", name) == EOF, errno);
state = ENTRY;
pthread_cond_signal(&cvar);
CHECK(pthread_mutex_unlock(&lockid));
return 0;
}
void *display() {
CHECK(pthread_mutex_lock(&lockid));
while(state != ENTRY) pthread_cond_wait(&cvar, &lockid);
printf("Name: %s\n", name);
CHECK(pthread_mutex_unlock(&lockid));
return 0;
}
int main() {
pthread_mutex_init(&lockid, NULL);
pthread_t id1, id2;
CHECK(pthread_create(&id1, NULL, &entry, NULL));
CHECK(pthread_create(&id2, NULL, &display, NULL));
CHECK(pthread_join(id1,NULL));
CHECK(pthread_join(id2,NULL));
return 0;
}