我的愿望
我构建了一个有趣的代码,必须在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
我遇到的问题
但最终结果是:
$ ./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)
我编写的方式
在我的ubuntu 14.14机器中,我只是输入了这个:
$ g++ lab.cpp -o lab -lpthread
我也尝试过-pthread
$g++ lab.cpp -o lab -pthread
但是,没有运气!!
我的代码
这是我的代码:
#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);
}
}
任何人都可以看到这个问题,我将非常感谢解决这个问题..
答案 0 :(得分:3)
看起来像一个错字。在两次调用pthread_create时都使用thread1。
iret1 = pthread_create( &thread1, 0, print_message_function1, (void*) message1);
iret2 = pthread_create( &thread1, 0, print_message_function2, (void*) message2);
所以pthread_join(thread2, 0);
注定要失败。
答案 1 :(得分:1)
这实际上只是相关信息,而不是答案,但遗憾的是,SO不支持评论中的代码。
您注意到代码的问题是一个简单的错字,但在我阅读现在accepted answer之前我没有看到。因为,我坐下来将代码重写为标准C ++,在这个过程中,错字消失了,完全没有注意到! :)我注意到的问题是你在代码中没有使用同步的输出语句,这可能导致输出行混合,我认为,正式是未定义的行为。
要解决此问题,您可以使用互斥锁。在重写代码时,我不会抽象出任何东西。但我认为你可以很容易地看到隐藏在代码行之间的自然抽象:
#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;
}
使用Visual C ++ 2015编译并运行良好。它不能使用MinGW-64 g ++ 5.1.0进行编译,因为它的std::thread
支持非常缺乏。我不知道它是否会在Unix-land中使用最新版本的g ++进行编译。