无法确定互斥锁是否启动?
Can't tell if Mutex Lock is kicking in or not?
我正在完成一项大学作业,任务是展示一个基本的互斥锁示例。我从来没有使用过任何形式的线程,所以我是一个在 C++ 中使用 POSIX 线程的初学者。
我想让程序做的是创建 1000 个线程,将全局整数递增 1000。
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <thread>
pthread_t threadArr[1000];
pthread_mutex_t lock;
// Global int to increment
int numberToInc = 0;
void* incByTwo(void*)
{
pthread_mutex_lock(&lock);
for(int j = 0; j < 1000; j++){
numberToInc += 1;
}
pthread_mutex_unlock(&lock);
return NULL;
}
int main()
{
//Creates 1000 threads with incByTwo func
for(int i = 0; i < 1000; i++){
pthread_create(&threadArr[i], NULL, incByTwo, NULL);
}
std::cout << "\n" << numberToInc << "\n";
return 0;
}
下面会产生一系列不同的结果,显然是因为线程是并发执行的吧?
现在,我通过插入
让它正常工作
for(int i = 0; i < 1000; i++){
pthread_join(threadArr[i], NULL);
}
在线程创建循环之后,但随后移除了互斥锁,它仍然有效。我一直在努力弄清楚 pthread_join 是如何工作的,但我有点迷路了。有什么建议吗?
整理了一种显示互斥锁的方法。所以当我在函数中输出全局变量时,如果没有互斥锁,它有可能显示出乱序的结果。
运行 带有互斥锁的数字范围,out 看起来像:
1000
2000
3000
... (etc)
10000
删除互斥锁后,输出的顺序可能会有所不同。
例如
1000
2000
4000
6000
3000
5000
7000
8000
9000
10000
虽然三个线程的最终结果是正确的,但是顺序乱了。在这个程序的上下文中,这并不重要,但我想如果它传递的是不一致的序列值,它会把事情搞砸吗?
pthread_t threadArr[10];
pthread_mutex_t lock;
int numberToInc = 0;
void* incByTwo(void*)
{
pthread_mutex_lock(&lock);
for(int j = 0; j < 1000; j++){
numberToInc += 1;
}
std::cout << numberToInc << "\n";
pthread_mutex_unlock(&lock);
return NULL;
}
int main()
{
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
for(int i = 0; i < 10; i++){
pthread_create(&threadArr[i], NULL, incByTwo, NULL);
}
pthread_join(threadArr[0], NULL);
return 0;
}
我正在完成一项大学作业,任务是展示一个基本的互斥锁示例。我从来没有使用过任何形式的线程,所以我是一个在 C++ 中使用 POSIX 线程的初学者。
我想让程序做的是创建 1000 个线程,将全局整数递增 1000。
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <thread>
pthread_t threadArr[1000];
pthread_mutex_t lock;
// Global int to increment
int numberToInc = 0;
void* incByTwo(void*)
{
pthread_mutex_lock(&lock);
for(int j = 0; j < 1000; j++){
numberToInc += 1;
}
pthread_mutex_unlock(&lock);
return NULL;
}
int main()
{
//Creates 1000 threads with incByTwo func
for(int i = 0; i < 1000; i++){
pthread_create(&threadArr[i], NULL, incByTwo, NULL);
}
std::cout << "\n" << numberToInc << "\n";
return 0;
}
下面会产生一系列不同的结果,显然是因为线程是并发执行的吧?
现在,我通过插入
让它正常工作 for(int i = 0; i < 1000; i++){
pthread_join(threadArr[i], NULL);
}
在线程创建循环之后,但随后移除了互斥锁,它仍然有效。我一直在努力弄清楚 pthread_join 是如何工作的,但我有点迷路了。有什么建议吗?
整理了一种显示互斥锁的方法。所以当我在函数中输出全局变量时,如果没有互斥锁,它有可能显示出乱序的结果。
运行 带有互斥锁的数字范围,out 看起来像:
1000
2000
3000
... (etc)
10000
删除互斥锁后,输出的顺序可能会有所不同。
例如
1000
2000
4000
6000
3000
5000
7000
8000
9000
10000
虽然三个线程的最终结果是正确的,但是顺序乱了。在这个程序的上下文中,这并不重要,但我想如果它传递的是不一致的序列值,它会把事情搞砸吗?
pthread_t threadArr[10];
pthread_mutex_t lock;
int numberToInc = 0;
void* incByTwo(void*)
{
pthread_mutex_lock(&lock);
for(int j = 0; j < 1000; j++){
numberToInc += 1;
}
std::cout << numberToInc << "\n";
pthread_mutex_unlock(&lock);
return NULL;
}
int main()
{
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
for(int i = 0; i < 10; i++){
pthread_create(&threadArr[i], NULL, incByTwo, NULL);
}
pthread_join(threadArr[0], NULL);
return 0;
}