[英]segmentation fault core dumped before pthread_join
My wish 我的希望
I build a funny code which must run as far on linux: 我构建了一个有趣的代码,该代码必须在Linux上运行得尽可能远:
$ ./lib Main: Creating threads Main: Waiting for threads to finish Hello #0 from Thread 1 Hello #0 from Thread 2 Hello #1 from Thread 1 Hello #1 from Thread 2 Hello #2 from Thread 1 Hello #2 from Thread 2 Hello #3 from Thread 1 Hello #3 from Thread 2 Hello #4 from Thread 1 Hello #4 from Thread 2 Hello #5 from Thread 1 Hello #5 from Thread 2 Hello #6 from Thread 1 Hello #6 from Thread 2 Hello #7 from Thread 1 Hello #7 from Thread 2 Hello #8 from Thread 1 Hello #8 from Thread 2 Hello #9 from Thread 1 Hello #9 from Thread 2 Thread 1 terminates Thread 2 terminates Main: Exiting
My problem experiented 我的问题遇到了
but it ends up with that : 但最终结果是:
$ ./lib Main: Creating threads Main: Waiting for threads to finish Hello #0 from Thread 1 Hello #0 from Thread 2 Hello #1 from Thread 1 Hello #1 from Thread 2 Hello #2 from Thread 1 Hello #2 from Thread 2 Hello #3 from Thread 1 Hello #3 from Thread 2 Hello #4 from Thread 1 Hello #4 from Thread 2 Hello #5 from Thread 1 Hello #5 from Thread 2 Hello #6 from Thread 1 Hello #6 from Thread 2 Hello #7 from Thread 1 Hello #7 from Thread 2 Hello #8 from Thread 1 Hello #8 from Thread 2 Hello #9 from Thread 1 Hello #9 from Thread 2 segmentation fault (core dumped)
The Way I compiled 我的编译方式
in my ubuntu 14.14 machine I just simply typed this: 在我的ubuntu 14.14机器上,我只是简单地输入以下内容:
$ g++ lab.cpp -o lab -lpthread
I have also tryied with -pthread, too 我也尝试过-pthread
$g++ lab.cpp -o lab -pthread
But, without luck!! 但是,没有运气!
My code 我的密码
this is my code: 这是我的代码:
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
using namespace std;
void *print_message_function1(void *ptr);
void *print_message_function2(void *ptr);
int main(){
cout << "\nMain: Creating threads" << endl;
cout << "Main: Waiting for threads to finish" << endl << endl;
pthread_t thread1, thread2;
char message1[] = " from Thread 1";
char message2[] = " from Thread 2";
int iret1, iret2;
iret1 = pthread_create( &thread1, 0, print_message_function1, (void*) message1);
iret2 = pthread_create( &thread1, 0, print_message_function2, (void*) message2);
pthread_join(thread1, 0);
pthread_join(thread2, 0);
cout << "Thread 0 terminates" << endl;
cout << "Thread 1 terminates" << endl;
cout << "Main: Exiting" << endl;
exit(0);
}
void *print_message_function1(void *ptr){
char *message;
message = (char*) ptr;
for(int i=0; i<10; i++){
cout << "Hello #" << i << message << endl;
sleep(1);
}
}
void *print_message_function2(void *ptr){
char *message;
message = (char*) ptr;
for(int i=0; i<10; i++){
cout << "Hello #" << i << message << endl;
sleep(1);
}
}
Any one can see that problem and i will be very thankfull to solve that problem.. 任何人都可以看到这个问题,我将非常感谢您解决这个问题。
Looks like a typo. 看起来像错字。 You use thread1 in both calls to pthread_create. 在对pthread_create的两个调用中都使用thread1。
iret1 = pthread_create( &thread1, 0, print_message_function1, (void*) message1);
iret2 = pthread_create( &thread1, 0, print_message_function2, (void*) message2);
So pthread_join(thread2, 0);
所以pthread_join(thread2, 0);
is pretty much doomed. 几乎注定了。
This is really just relevant information , not an answer as such, but unfortunately SO does not support code in comments. 这实际上只是相关信息 ,而不是答案,但是不幸的是,SO不支持注释中的代码。
The problem that you noticed with your code was a simple typo, but I didn't see that until I read the now accepted answer . 您在代码中注意到的问题是一个简单的错字,但是直到我阅读了现在可以接受的答案之后 ,我才发现它。 For, I sat down and rewrote the code to standard C++, and in that process the typo disappeared, completely unnoticed! 为此,我坐下来并将代码重写为标准C ++,在那一过程中,打字错误消失了,完全没有被注意到! :) The problem that I did note was that you're using output statements without synchronization in your code, and that might cause output lines to be mixed, and is, I think, formally Undefined Behavior. :)我确实注意到的问题是,您在代码中使用的输出语句没有同步,这可能导致输出行混合,并且我认为这是正式的未定义行为。
To fix that you can use a mutex . 要解决此问题,您可以使用互斥锁 。 In this rewrite of your code I do not abstract up anything. 在您的代码重写中,我没有抽象任何内容。 But I think you can readily see the natural abstractions that hide between the code lines here: 但是我想您可以在这里轻松地看到隐藏在代码行之间的自然抽象:
#include <chrono> // operator""ms
#include <iostream>
#include <mutex>
#include <stdlib.h>
#include <thread>
using namespace std;
mutex output_ownership;
void print_message_function1( char const* const message )
{
for( int i = 0; i < 10; ++i )
{
{
lock_guard<mutex> mux( output_ownership );
cout << "Hello #" << i << message << endl;
}
this_thread::sleep_for( 1ms );
}
lock_guard<mutex> mux( output_ownership );
cout << "Thread 0 terminates" << endl;
}
void print_message_function2( char const* const message )
{
for( int i = 0; i < 10; ++i )
{
{
lock_guard<mutex> mux( output_ownership );
cout << "Hello #" << i << message << endl;
}
this_thread::sleep_for( 1ms );
}
lock_guard<mutex> mux( output_ownership );
cout << "Thread 1 terminates" << endl;
}
auto main() -> int
{
cout << "Main: Creating threads" << endl;
cout << "Main: Waiting for threads to finish" << endl << endl;
thread thread1( print_message_function1, " from Thread 1" );
thread thread2( print_message_function2, " from Thread 2" );
thread1.join();
thread2.join();
cout << "Main: Exiting" << endl;
}
This compiles and runs fine with Visual C++ 2015. It does not compile with MinGW-64 g++ 5.1.0, because its std::thread
support is very much lacking. 这可以在Visual C ++ 2015中编译并正常运行。它不能与MinGW-64 g ++ 5.1.0编译,因为非常缺乏对std::thread
支持。 I do not know whether it will compile with latest version of g++ in Unix-land. 我不知道它是否可以在Unix-land上使用最新版本的g ++进行编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.