스레드로 길이 50의 배열을 만들고 싶습니다.이 작업을 완료하면 두 번째 스레드에서 모든 X 초의 첫 번째 값을 인쇄하고 싶습니다. 한편 첫 번째 스레드는 다음 배열을 계산할 수 있습니다.pthread windows crash C++
스레드는 일부 임시 변수에서 계산 된 배열의 일부 값을 복사하려고 시도 할 때까지 작동합니다. 컴파일 오류가 없지만 프로그램을 실행할 때 Windows 충돌 마사지를받습니다.
스레드가없는 경우 double * newarray(); 기능이 작동합니다. 수동으로 할당되고 데이터로 채워진 배열을 반환합니다.
무엇이 여기에 있습니까?
스레드 1 :
double *newarray();
void *computingU(void *)
{
double * U_tmp;
while (true)
{
pthread_mutex_lock(&mutexU);
memcpy(U_tmp,newarray(),sizeof(double)*Ulenght);
while (!Usent);
Usent = false;
memcpy(Ucmd,U_tmp,sizeof(double)*Ulenght);
pthread_mutex_unlock(&mutexU);
Ucomputed = true;
}
}
스레드 2 :
void *sendingCMD(void *) {
double * U_tmp;
while (true)
{
while (!Ucomputed);
Ucomputed = false;
pthread_mutex_lock(&mutexU);
memcpy(U_tmp,Ucmd,sizeof(double)*Ulenght);
pthread_mutex_unlock(&mutexU);
Usent = true;
for (int i = 0; i<Ulenght; i++)
{
printf("i= %d, u= %f", i, U_tmp[i]);
sleep(sleepTime) ;
}
}
}
홈페이지 :
#include <pthread.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
using namespace std;
bool Ucomputed = false, Usent = true;
double * Ucmd;
pthread_mutex_t mutexU = PTHREAD_MUTEX_INITIALIZER;
unsigned int Ulenght = 1;
int sleepTime = 1;
int main(void)
{
#ifdef DEBUG_THREAD
int rc1, rc2;
pthread_t thread1, thread2;
/* Create independent threads each of which will execute functionC */
if((rc1=pthread_create(&thread1, NULL, &computingU, NULL))) {
printf("Thread creation failed: %d\n", rc1);
}
if((rc2=pthread_create(&thread2, NULL, &sendingCMD, NULL)))
{
printf("Thread creation failed: %d\n", rc2);
}
#endif //
sleep(10);
while (true);
}
왜 '컴퓨 팅 U'에서 뮤텍스를 두 번 잠그고 있습니까? – programmerjake
그 코드를 cpy - 내 실수입니다. – Elod