Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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 UNIX Pthreads&;互斥;程序锁定_C_Multithreading - Fatal编程技术网

C UNIX Pthreads&;互斥;程序锁定

C UNIX Pthreads&;互斥;程序锁定,c,multithreading,C,Multithreading,以下场景: 我们应该使x线程最多。我们的主函数应该使用指向函数“makeThreads”的指针创建一个新线程。此函数最多应包含2个线程,具体取决于已有的线程数。比赛条件是要避免的。 我卡住了。我不太确定如何解决我遇到的问题,部分原因是我无法识别问题本身 非常感谢您的建议 #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <time.h> #define MAX

以下场景:

我们应该使x线程最多。我们的主函数应该使用指向函数“makeThreads”的指针创建一个新线程。此函数最多应包含2个线程,具体取决于已有的线程数。比赛条件是要避免的。 我卡住了。我不太确定如何解决我遇到的问题,部分原因是我无法识别问题本身

非常感谢您的建议

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>

#define MAX_THR 20

pthread_mutex_t mutex;
int threadCount = 0;

int randomNbr(){
    int number = (rand() % 10) + 1;
    return number;
}

void *makeThreads(void* number){

    int rndnmb = *((int *) number);

    pthread_mutex_lock(&mutex);

    sleep(rndnmb);
    pthread_t threadDummy;
    int thread1, i, threadID, rndnbr;

    threadID = threadCount;
    printf("Hello from Thread %d!\n", threadID);

    for(i = 0; i < 2; i++){
        if(threadCount < MAX_THR){
            rndnbr = randomNbr();
            int *rnd = &rndnbr;
            threadCount++;
            thread1 = pthread_create(&threadDummy, NULL, *makeThreads, (void *) rnd);
            pthread_join(threadDummy, NULL);

        }
    }

    pthread_mutex_unlock(&mutex);
    printf("Goodbye from Thread %d!\n", threadID);
}

int main(){

    int t1, rndnbr;

    pthread_t threadOne;
    pthread_mutex_init(&mutex, NULL);

    srand(time(NULL));
    rndnbr = randomNbr();
    int *rnd = &rndnbr;
    threadCount++;
    t1 = pthread_create(&threadOne, NULL, *makeThreads, (void *) rnd);
    pthread_join(threadOne, NULL);

}
#包括
#包括
#包括
#包括
#定义最大值为20
pthread_mutex_t mutex;
int threadCount=0;
int randombr(){
整数=(rand()%10)+1;
返回号码;
}
void*makeThreads(void*number){
int rndnmb=*((int*)编号);
pthread_mutex_lock(&mutex);
睡眠(rndnmb);
pthread\u t threadDummy;
int thread1,i,threadID,rndnbr;
threadID=线程数;
printf(“来自线程%d!\n的你好”,线程ID);
对于(i=0;i<2;i++){
if(螺纹数<最大螺纹数){
rndnbr=randomNbr();
int*rnd=&rndnbr;
threadCount++;
thread1=pthread_create(&threadDummy,NULL,*makeThreads,(void*)rnd);
pthread_join(threadDummy,NULL);
}
}
pthread_mutex_unlock(&mutex);
printf(“线程%d的再见!\n”,线程ID);
}
int main(){
int t1,rndnbr;
pthread_t threadOne;
pthread_mutex_init(&mutex,NULL);
srand(时间(空));
rndnbr=randomNbr();
int*rnd=&rndnbr;
threadCount++;
t1=pthread_create(&threadOne,NULL,*makeThreads,(void*)rnd);
pthread_join(threadOne,NULL);
}

创建一个线程,该线程锁定互斥锁,并创建一个新线程,该线程执行相同的操作。第一个锁会阻止第二个锁,并等待第二个锁结束。所以,它不会等到互斥锁解锁,而是会再次锁定他?我是否误解了互斥锁?不,第二个线程等待互斥锁(在pthread_mutex_锁处),第一个线程等待第二个线程的结束(在pthread_连接处)。因此,它们相互阻塞。因此,如果我在互斥体之外创建线程,并在互斥体中获取相关数据(threadcount等),它就不应该再阻塞了?是的,别忘了在函数末尾返回一些东西。