[英]pthread windows crash C++
I would like to create an array of length 50 with a thread, when this is done I would like to print some of the first values in every X seconds with a second thread. 我想用一个线程创建一个长度为50的数组,完成后,我想用第二个线程每X秒打印一些第一个值。 In the meanwhile the first thread can compute the next array. 同时,第一个线程可以计算下一个数组。
The threads are functional until I try to copy some values from the computed array in some temporary variables. 直到我尝试从一些临时变量中复制计算数组中的某些值之前,这些线程才起作用。 I have no compilation error but when I run the program I get a windows crash massage. 我没有编译错误,但是当我运行程序时,我得到了Windows崩溃按摩。
Without threads the double *newarray(); 没有线程, 双* newarray(); function works. 功能起作用。 Returns an array which was manually allocated and filled with data. 返回一个手动分配并填充数据的数组。
What am I missing here? 我在这里想念什么?
Thread 1: 线程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;
}
}
Thread 2: 线程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) ;
}
}
}
Main: 主要:
#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);
}
Lets take the first thread in the computingU
function, there you have a local variable: 让我们以computingU
函数中的第一个线程computingU
,那里有一个局部变量:
double * U_tmp;
Later you use this variable: 稍后,您使用此变量:
memcpy(U_tmp,newarray(),sizeof(double)*Ulenght);
But nowhere do you initialize the variable, so it doesn't point to anything. 但是您无处初始化变量,因此它没有指向任何内容。 As uninitialized (non-static) local variables have indeterminate value, so the pointer U_tmp
will point to a seemingly random location. 由于未初始化(非静态)的局部变量具有不确定的值,因此指针U_tmp
将指向看似随机的位置。 Writing there will lead to undefined behavior and most likely your crash. 在那写东西会导致不确定的行为 ,很可能导致崩溃。
And you have the same problem in the other thread. 而且您在另一个线程中遇到了同样的问题。
Look at: 看着:
double * U_tmp;
You never set the pointer to anything, and then attempt to memcpy() data into it. 您永远不会将指针设置为任何内容,然后尝试将memcpy()数据放入其中。 That will 100% crash every time. 每次都会100%崩溃。 If it doesn't your OS is broken. 如果不是,则您的操作系统已损坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.