2

I'm asking your help to better understand some things about C++ threads. I'm using codeblocks with GNU GCC compiler, so since there is no pthread.h (AFAIK) I'm using process.h and _beginthread. I'd like that the function that the thread will execute be reusable. There is a while(!finished) statement in that function, so I can set finished to true when I'd like that the thread ends. Everytime that I click a button, I'd like that the finished variable will be set to true, then I'd like to wait till the thread die, and then I'd like to start another thread using the same function. So, how can I wait the end of the first thread ? Sorry if this is a bit complicated to understand, but I can't explain it better.

I hope that somebody can help me, I'm new to threads world, and I really need you help. Thank you very much.

Luca D'Amico
  • 3,192
  • 2
  • 26
  • 38
  • C++ has a standard threading library with a `join` function. – chris Jul 16 '14 at 20:20
  • 1
    The IDE has nothing to do with what libraries are available. Check your compiler and use the C++11 thread API. – E_net4 Jul 16 '14 at 20:21
  • Sorry I'm new to threads world, can you please tell me what std lib are you talking about ? I'm using codeblocks with gnu gcc, so if you are talking about pthread.h, I'm sorry but I can't use it. – Luca D'Amico Jul 16 '14 at 20:21
  • `pthread.h` is not a standard header. It does not take much searching at all to find what the standard C++ thread header is, but http://en.cppreference.com/w/cpp/header/thread – chris Jul 16 '14 at 20:22
  • yeah I was aware of that, but this is C++11. That's why I used _beginthread instead. – Luca D'Amico Jul 16 '14 at 20:24
  • Unless there is something that will not let you use a modern C++ compiler, you should be fine with using C++11. – E_net4 Jul 16 '14 at 20:24
  • Okay then I'll try it thanks. Just to know, isn't there anything that let me wait for the end of the thread without using C++11 ? – Luca D'Amico Jul 16 '14 at 20:26
  • 1
    @LucaD'Amico, `_beginthread` reutrns a handle suitable for using with `WaitForSingleObject`. Don't quote me on that being the best way, though. I've never used `_beginthread`. Actually, reading the remarks, it looks like `_beginthreadex` is the only one whose handle is suitable for use in synchronization functions. – chris Jul 16 '14 at 20:28

1 Answers1

1

Your post mentions WinAPI. Windows has its own threads library that is around for decades. It is reliable and very simple to use. Example of the call:

HANDLE hThread = CreateThread(nullptr, 0, thread_entry_point, this, 0, &m_dwThreadId);

This will work on any Windows platform. It is worth thinking about this once you do something for Windows.

I personally used Windows threads and processes for decades. They always works fine, exactly as the docs say. Our days PCs can easily start several thousands of threads without any issue.

Besides that the only really portable solution will be C++11 threads.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51