1

Hi I'm starting with threads in c++ in Windows.

#include <iostream>
#include <thread>
#include <windows.h>
using namespace std;
static const int num_threads = 2;

CRITICAL_SECTION critSection;
int thread1 ( int id ) {

    EnterCriticalSection ( &critSection );
    cout <<"thread1 had id:  " << id<<endl;
    LeaveCriticalSection ( &critSection );
    return 15;
}

void thread2() {
    EnterCriticalSection ( &critSection );
    cout << "Sleeping"<< endl;
    LeaveCriticalSection ( &critSection );
    Sleep ( 20 );
    EnterCriticalSection ( &critSection );
    cout << "No more"<< endl;
    LeaveCriticalSection ( &critSection );
}

int main() {
    thread t[num_threads];
    InitializeCriticalSection ( &critSection );

    t[0]=thread ( thread2);
    t[1] = thread ( thread1, 1 );
    EnterCriticalSection ( &critSection );
    LeaveCriticalSection ( &critSection );
    t[0].join();
    t[1].join();


    DeleteCriticalSection ( &critSection );
    return 0;
}

so my question is simple, how do I get return value from thread1, and second question is, is this correct way to do multithreading in C++?

  • 6
    You should consider using standard threading functionalities such as [`std::thread`](http://en.cppreference.com/w/cpp/thread/thread), [`std::mutex`](http://en.cppreference.com/w/cpp/thread/mutex), [`std::lock_guard`](http://en.cppreference.com/w/cpp/thread/lock_guard) and [`std::async`](http://en.cppreference.com/w/cpp/thread/async). – François Andrieux Apr 06 '17 at 14:43
  • I'd use a function callback to "return" something!? – xander Apr 06 '17 at 14:44
  • Threads can return results by using reference parameters. You can add an additional "return" argument, taken by reference, to your asynchronous function and use that object to store your result. For simple threading tasks, use `std::async` which allows you to return a function's result. – François Andrieux Apr 06 '17 at 14:46
  • Some people, when confronted with a problem, think, "I know, I'll use threads," and then two they hav erpoblesms. – Slava Apr 06 '17 at 14:50
  • @Slava, Threads can provide a clean solution to some problems. The fact that they can be misused should not stand in the way of somebody who wants to learn how they _should_ be used. – Solomon Slow Apr 06 '17 at 15:22

1 Answers1

1

You can look at future class. This is an example from cppreference.com:

#include <iostream>
#include <future>
#include <thread>

int main()
{
    // future from a packaged_task
    std::packaged_task<int()> task([](){ return 7; }); // wrap the function
    std::future<int> f1 = task.get_future();  // get a future
    std::thread(std::move(task)).detach(); // launch on a thread

    // future from an async()
    std::future<int> f2 = std::async(std::launch::async, [](){ return 8; });

    // future from a promise
    std::promise<int> p;
    std::future<int> f3 = p.get_future();
    std::thread( [&p]{ p.set_value_at_thread_exit(9); }).detach();

    std::cout << "Waiting..." << std::flush;
    f1.wait();
    f2.wait();
    f3.wait();
    std::cout << "Done!\nResults are: "
              << f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n';
}
alangab
  • 849
  • 5
  • 20