Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

C++ std::threads are causing exceptions on process shutdown

alon_b_
Beginner
576 Views


Hey,

I have the following issues that can be represented by both those 2 links:

http://stackoverflow.com/questions/266168/simple-example-of-threading-in-c

http://stackoverflow.com/questions/6485705/why-does-this-simple-stdthread-example-not-work

 

the problem is that I am getting std::exception on shutdown and negative return code..

the question is does the -pthread flag work with icpc? (seems like it doesn't)

how should I make this work?

 

Thanks,

Alon

0 Kudos
3 Replies
Shenghong_G_Intel
576 Views

Hi alon,

Why -lpthread does not work with icpc? It works.

I do not have issues to build the code in the first link, please post your code and compiler version/options used.

$ cat temp.cpp
#include <string>
#include <iostream>
#include <thread>

using namespace std;

//The function we want to make the thread run.
void task1(string msg)
{
    cout << "task1 says: " << msg;
    }

    int main()
    {
        // Constructs the new thread and runs it. Does not block execution.
            thread t1(task1, "Hello");

                //Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
                    t1.join();
                    }
$ icc temp.cpp -std=c++11 -lpthread
$ ./a.out
task1 says: Hello$

Thanks,

Shenghong

0 Kudos
alon_b_
Beginner
576 Views

not lpthread, -pthread for compiler flags, the q is what is the return code, not if it works(it does work)

0 Kudos
pbkenned1
Employee
576 Views

icc 15.0 supports the -pthread option.  The links you provided contain various code examples.  If you still have a problem with icc 15.0, kindly post YOUR example and we'll investigate further.

Patrick

[U536960]$ cat U536960.cpp
#include <string>
#include <iostream>
#include <thread>

using namespace std;

//The function we want to make the thread run.
 void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
// Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello \n");
    t1.join();
    return 0;
}
[U536960]$ icc -V
Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.1.133 Build 20141023
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

[U536960]$ icc -std=c++11 U536960.cpp -pthread && ./a.out
task1 says: Hello
[U536960]$

0 Kudos
Reply