Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 线程问题_C_Multithreading_Semantics - Fatal编程技术网

C 线程问题

C 线程问题,c,multithreading,semantics,C,Multithreading,Semantics,我想做10次,扫描号码并再次打印。我该怎么做 #include <stdio.h> #include <pthread.h> #include <semaphore.h> sem_t m; int n; void *readnumber(void *arg) { scanf("%d",&n); sem_post(&m); } void *writenumber(void *arg) { //

我想做10次,扫描号码并再次打印。我该怎么做

#include <stdio.h>

#include <pthread.h>
#include <semaphore.h>

sem_t m;
int n;

void *readnumber(void *arg)
{
        scanf("%d",&n);
        sem_post(&m);
}

void *writenumber(void *arg)
{   
    //int x =3;
    //while(x>0)
    //{
        //x = x-1;
        sem_wait(&m);
        printf("%d",n);

    //}
}

int main(){
    pthread_t t1, t2;
    sem_init(&m, 0, 0);
    pthread_create(&t2, NULL, writenumber, NULL);
    pthread_create(&t1, NULL, readnumber, NULL);
    pthread_join(t2, NULL);
    pthread_join(t1, NULL);
    sem_destroy(&m);
    return 0;
}
#包括
#包括
#包括
扫描电镜;
int n;
void*readnumber(void*arg)
{
scanf(“%d”和“&n”);
sem_post&m;
}
void*writenumber(void*arg)
{   
//int x=3;
//而(x>0)
//{
//x=x-1;
sem_wait&m;
printf(“%d”,n);
//}
}
int main(){
pthread_t t1,t2;
sem_init(&m,0,0);
pthread_create(&t2,NULL,writenumber,NULL);
pthread_create(&t1,NULL,readnumber,NULL);
pthread_join(t2,NULL);
pthread_join(t1,NULL);
扫描电镜(sem)和扫描电镜(m);
返回0;
}

我不完全确定你在问什么,但一般来说,如果你想让某件事发生特定次数,你需要使用一个
for
循环,如下所示:

for(int i = 0; i < 10; i++) {
//whatever you want to happen 10 times goes here
}
for(int i=0;i<10;i++){
//任何你想发生10次的事情都会发生在这里
}

我感到困惑的原因是,有人会在不知道
for
循环是什么的情况下想出如何创建POSIX线程,这有点奇怪。

我不完全确定你在问什么,但一般来说,如果你想让某件事发生特定次数,你需要使用
for
循环,如下所示:

for(int i = 0; i < 10; i++) {
//whatever you want to happen 10 times goes here
}
for(int i=0;i<10;i++){
//任何你想发生10次的事情都会发生在这里
}

我感到困惑的原因是,有人在不知道什么是
for
循环的情况下就知道了如何创建POSIX线程,这有点奇怪。

我不确定这种情况是否真的紧急到需要多个感叹号。double post:我不确定这种情况是否真的紧急到需要多个感叹号感叹号。双柱: