如何修复下一个线程更正确?使用线程
How to fix the next thread to be more correct? Using Pthread
我正在研究 PThread 的使用。
主进程打开相机,得到一个矩阵。然后调用机器人中 运行 作业的线程,我希望它是并行的。基本上它可以运行。但还是觉得不够专业-因为bool
.
在下面的代码中,这是一个示例(fprintf
)。
我很想知道如何在不损害并行性的情况下修复它。
在接下来的代码中,我不会显示对机器人或相机打开的调用。
感觉需要互斥量
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <opencv2/opencv.hpp>
#include <unistd.h> /// for sleep
bool inThread = false;
void *print_message_function( void *ptr );
int main()
{
char mkey = 0;
pthread_t thread1;
char *message1 = "Thread 1";
int iret1;
cv::Mat bgr_image = imread("image.bmp",cv::IMREAD_COLOR);
while(mkey!=27){
if(!inThread){
inThread = true;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
}
printf("In Main");
imshow("mat", bgr_image);
mkey = cv:: waitKey(5);
}
return 0;
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
sleep(2);
inThread = false;
pthread_exit(NULL);
}
代码很好用,不掉,但感觉不专业。有没有可能当你更新标志时,它会检查标志中的内容并掉落?
inThread
同时 read/written 所以它的访问应该被保护。
例如,使用互斥锁可以像下面这样完成。
定义一个全局互斥锁并初始化它:
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
包括 errno
以便能够为 pthread_*()
调用做方便的错误 checking/logging:
#include <errno.h>
改变这个
if(!inThread){
inThread = true;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
}
成为
errno = pthread_mutex_lock(&m);
if (errno) {
perror("pthread_mutex_lock() failed");
exit(EXIT_FAILURE);
}
if (!inThread) {
inThread = true;
errno = pthread_mutex_unlock(&m);
if (errno) {
perror("pthread_mutex_unlock() failed");
exit(EXIT_FAILURE);
}
...
}
else {
errno = pthread_mutex_unlock(&m);
if (errno) {
perror("pthread_mutex_unlock() failed");
exit(EXIT_FAILURE);
}
}
并更改此
inThread = false;
成为
errno = pthread_mutex_lock(&m);
if (errno) {
perror("pthread_mutex_lock() failed");
exit(EXIT_FAILURE);
}
inThread = false;
errno = pthread_mutex_unlock(&m);
if (errno) {
perror("pthread_mutex_unlock() failed");
exit(EXIT_FAILURE);
}
我正在研究 PThread 的使用。
主进程打开相机,得到一个矩阵。然后调用机器人中 运行 作业的线程,我希望它是并行的。基本上它可以运行。但还是觉得不够专业-因为bool
.
在下面的代码中,这是一个示例(fprintf
)。
我很想知道如何在不损害并行性的情况下修复它。
在接下来的代码中,我不会显示对机器人或相机打开的调用。
感觉需要互斥量
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <opencv2/opencv.hpp>
#include <unistd.h> /// for sleep
bool inThread = false;
void *print_message_function( void *ptr );
int main()
{
char mkey = 0;
pthread_t thread1;
char *message1 = "Thread 1";
int iret1;
cv::Mat bgr_image = imread("image.bmp",cv::IMREAD_COLOR);
while(mkey!=27){
if(!inThread){
inThread = true;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
}
printf("In Main");
imshow("mat", bgr_image);
mkey = cv:: waitKey(5);
}
return 0;
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
sleep(2);
inThread = false;
pthread_exit(NULL);
}
代码很好用,不掉,但感觉不专业。有没有可能当你更新标志时,它会检查标志中的内容并掉落?
inThread
同时 read/written 所以它的访问应该被保护。
例如,使用互斥锁可以像下面这样完成。
定义一个全局互斥锁并初始化它:
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
包括
errno
以便能够为pthread_*()
调用做方便的错误 checking/logging:#include <errno.h>
改变这个
if(!inThread){ inThread = true; iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); }
成为
errno = pthread_mutex_lock(&m); if (errno) { perror("pthread_mutex_lock() failed"); exit(EXIT_FAILURE); } if (!inThread) { inThread = true; errno = pthread_mutex_unlock(&m); if (errno) { perror("pthread_mutex_unlock() failed"); exit(EXIT_FAILURE); } ... } else { errno = pthread_mutex_unlock(&m); if (errno) { perror("pthread_mutex_unlock() failed"); exit(EXIT_FAILURE); } }
并更改此
inThread = false;
成为
errno = pthread_mutex_lock(&m); if (errno) { perror("pthread_mutex_lock() failed"); exit(EXIT_FAILURE); } inThread = false; errno = pthread_mutex_unlock(&m); if (errno) { perror("pthread_mutex_unlock() failed"); exit(EXIT_FAILURE); }