[英]Waiting for pthread_create to finish without using pthread_join
[英]How do I synchronize the threads without using pthread_join()?
我创建了一个简单的 C++ 程序,用于日志记录。 我正在 for 循环中创建线程,该循环在test.cpp
运行 1000 次,这是驱动程序文件。 pthread_create
调用打印功能tt.cpp
其中写入参数即i中的输入,在file.txt
。 我希望同步线程。
我曾尝试使用同步线程的pthread_join
,但我希望在不使用 join 的情况下同步线程。
我知道如果一个线程被锁定,很多线程将等待直到它被解锁,并且在该线程被解锁后,任何一个等待的线程都会锁定该函数。 因此,我尝试在tt.cpp
使用静态整数变量并尝试将其与输入参数进行比较,以便我可以使用pthread_cond_wait
和pthread_wait_signal
但我在比较时遇到了分段错误。
/* if(j == *((int *)input)))
This comparison gave me a segmentation fault */
-------------------------test.cpp---------------------------------
#include "thr.h"
pthread_mutex_t loc;
FILE *thePrintFile = NULL;
int main()
{
pthread_mutex_init(&loc,NULL);
pthread_t p;
thePrintFile = fopen("file.txt","r+");
for(int i =0; i<1000;i++)
{
pthread_create(&p,NULL,print,(void *)i);
}
for(int k = 0; k<100000;k++);
/* i have used it to ensure that the main thread
doesn't exit before the pthreads finish writing */
return 0;
}
------------------------tt.cpp------------------------------------
#include "thr.h"
extern pthread_mutex_t loc;
extern FILE *thePrintFile;
void* print(void *input)
{
if(pthread_mutex_trylock(&loc) == 0)
{
fprintf(thePrintFile,"%s%d\n",(int *)input);
fflush(thePrintFile);
pthread_mutex_unlock(&loc);
}
}
-----------------------------thr.h--------------------------------
#ifndef THR_H_INCLUDED
#define THR_H_INCLUDED
void* print(void *input);
#endif
下面给出的是 file.txt 的一部分。
1
5
9
10
11
12
13
14
15
16
18
19
20
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
21
201
202
要正确同步线程,您需要使用同步设备,例如条件变量。 由于您使用的是 C++,因此最好使用其内置线程而不是原始 pthread API。
例如,此代码同步 1000 个线程,以便每个线程使用条件变量和互斥对打印自己的 ID,以确保按顺序打印 ID,由所有共享的单独变量跟踪。 它不是很有效,因为所有线程都为同一个互斥体而斗争,但它可以正常工作。 可以通过为每个线程创建一个条件变量来提高效率,但正确执行它需要更好地理解您的实际用例。
#include <thread>
#include <mutex>
#include <condition_variable>
#include <iostream>
#include <vector>
static std::mutex mutex;
static std::condition_variable condvar;
static int whos_next;
static void work(int id) {
std::unique_lock<std::mutex> lock{mutex};
// wait for our turn
condvar.wait(lock, [=]() { return whos_next == id; });
// it's our turn now - print our thread ID
std::cout << id << '\n';
++whos_next;
condvar.notify_all(); // notify the next thread to run
}
int main() {
std::vector<std::thread> threads;
for (int i = 0; i < 1000; i++)
threads.push_back(std::thread([=]() { work(i); }));
for (auto &t: threads)
t.join();
}
请注意,上述代码对join
的使用不是为了使线程彼此同步(它们使用互斥锁/条件在它们之间进行同步),而是出于其预期目的:在退出main()
之前等待线程完成。 C++ 甚至要求您在销毁std::thread
对象之前执行此操作,因此删除连接会导致程序终止。 您可以轻松证明线程不依赖于join
进行同步,例如通过在 join 之前插入 sleep 或以相反的顺序加入它们。
我在您的代码中看到多个问题。
然后让我们看一下您尝试进行的比较:
if(j == *((int *)input)))
j 显然是整数。 输入为空 *。 在基础输入是int。 因为不能保证将 int 转换为 void * 是安全的,所以将其转换回也不安全。 想象一下 int 是 64 位,指针是 32 位。 如果您将 int 转换为 void * 您将丢失 32 位。 然后你从 32 位转换到 64 位,所以你添加了 32 位的 0。 显然,这会带来问题。
考虑使用 C++11,这将大大改进您的代码。 https://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.