[英]Why is the output of my pthread and semaphore program blank?
The following code is written in C, and when I run the code nothing outputs to the screen.下面的代码写在C,当我运行这段代码时,屏幕上没有任何输出。 I am not coding in an IDE, so I cannot tell where the error is located.
我没有在 IDE 中编码,所以我不知道错误在哪里。 Please, any help is appreciated.
请,任何帮助表示赞赏。
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
int counter=0;//this is the count variable and is the variable that the treads will edit, making it the critical section
static sem_t semaphore;//this creates a pointer for our semaphore variable
void *increaseCounter();
void *decreaseCounter();
int main()
{
sem_init(&semaphore, 0, 1);
pthread_t t1,t2;
pthread_create(&t1, NULL, increaseCounter, NULL);
pthread_create(&t2, NULL, decreaseCounter, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
sem_destroy(&semaphore);
}
void *increaseCounter()
{
while(1)
{
sem_wait(&semaphore);
counter++;
printf("\n%d\n",counter);
sem_post(&semaphore);
}
}
void *decreaseCounter()
{
while(1)
{
sem_wait(&semaphore);
counter--;
printf("\n%d\n",counter);
sem_post(&semaphore);
}
}
Run but its a loop, might gonna help you运行但它是一个循环,可能会帮助你
gcc -g -Wall -o test NAME.c -lpthread
./test 。/测试
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
//this is the count variable and is the variable that
//the treads will edit, making it the critical section
static int counter=0;
//this creates a pointer for our semaphore variable
static sem_t semaphore;
void *increaseCounter();
void *decreaseCounter();
int main()
{
sem_init(&semaphore, 0, 1);
pthread_t t1,t2;
pthread_create(&t1, NULL, increaseCounter, NULL);
pthread_create(&t2, NULL, decreaseCounter, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
sem_destroy(&semaphore);
}
void *increaseCounter()
{
while(1)
{
sem_wait(&semaphore);
counter++;
printf("\n%d\n",counter);
sem_post(&semaphore);
}
}
void *decreaseCounter()
{
while(1)
{
sem_wait(&semaphore);
counter--;
printf("\n%d\n",counter);
sem_post(&semaphore);
}
}
You have to make a couple of changes in your code will run fine.您必须对代码进行一些更改才能正常运行。
Always read the documentation before writing something.在写东西之前一定要阅读文档。 Here I'm attaching some documents that may help u in the future Synchronizing Threads with POSIX Semaphores .
在这里,我附上了一些文档,这些文档可能会在将来帮助您 使用 POSIX 信号量同步线程。
This may improve your understanding of threadsPOSIX thread (pthread) libraries .这可能会提高您对线程POSIX 线程 (pthread) 库的理解。
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
int counter=0;
sem_t semaphore;
void *increaseCounter();
void *decreaseCounter();
int main()
{
sem_init(&semaphore, 0, 1);
pthread_t t1,t2;
pthread_create(&t1, NULL, increaseCounter, NULL);
pthread_create(&t2, NULL, decreaseCounter, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
sem_destroy(&semaphore);
}
void *increaseCounter()
{
while(1)
{
sem_wait(&semaphore);
counter++;
printf("\n%d\n",counter);
sem_post(&semaphore);
}
}
void *decreaseCounter()
{
while(1)
{
sem_wait(&semaphore);
counter--;
printf("\n%d\n",counter);
sem_post(&semaphore);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.