0

Just a quick question on which I didn't find clear answer:

Is it completely safe to create C++ std::threads, in which I use pthread_mutex_t for locking critical sections?

EDIT: Mutexes locks parts of code to be safe from race conditioning. So I think it should have nothing to do with the matter of choosing C or C++ threads.

scarface
  • 574
  • 6
  • 20
  • 1
    I don't know `std::thread`s but, I wouldn't do that even if I knew that it was very safe. Because `std::thread` is perhaps an abstraction that uses abstracted mutexes and other thread related things, just DON'T DO THAT. Instead read `std::thread`'s documentation and do it the right way, don't mix things like that ever. – Iharob Al Asimi Mar 16 '17 at 23:35

3 Answers3

1

Don't do it, that's what std::mutex's are for, and if you want to further investigate you can google about all std::thread related things.

I consider it important to point out, that I have never used std::threads in my life, but it's just a reasonable assumption that if there exists, std::thread, there must be a std::mutex, I typed std::mutex in google and the page in the link I posted in this answer appeared.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • I know that `std::mutex` exists.. I am asking on combining two things in the title of question – scarface Mar 16 '17 at 23:44
  • The answer is don't do it, even if it works. Don't combine them. Either, use `pthread`s or `std::thread`s, but not both in the same program. It doesn't matter if it works, or if `std::mutex` is built with `pthread_mutex_t`, it's a matter of design and also good taste, DON'T, JUST DON'T! Not to mention, interoperability among the objects you use in your code. – Iharob Al Asimi Mar 16 '17 at 23:46
  • Okay, thank you. You know why I am asking? In C++ you don't have implicitly implemented semaphores. Either you have to use C sempahores or go with some obstacle or something like that. – scarface Mar 16 '17 at 23:47
  • @scarface Interesting, then don't use c++ – Iharob Al Asimi Mar 16 '17 at 23:56
  • 1
    Not a solution - your last comment is a strange statement. – scarface Mar 16 '17 at 23:58
  • @scarface I am sorry, but it's not! If you can't use the language because it lacks this or that, then use another language. If you can't use `std::thread` because then, you have no semaphores, use `pthreads`, and so on. Don't use things that have the potential to be incompatible together. Also, [see why there are no standard semaphores](http://stackoverflow.com/a/4793662/1983495), because it's a simple thing to write one from the available objects, and you probably don't need one anyway – Iharob Al Asimi Mar 17 '17 at 00:02
  • Whatever, thanks. I will wait for some time for some answers and then set your answer as the right one – scarface Mar 17 '17 at 00:06
1

For portability, use std::thread and its associated structures like std::mutex. Minimize use of e.g. native handles etc. Using the std:: mechanisms also makes your code less buggy, by using RAII. Without it, even experienced programmers forget to unlock locks at e.g. exceptions.

But here is a question similar to yours: Is it safe to mix pthread.h and C++11 standard library threading features?

Community
  • 1
  • 1
Erik Alapää
  • 2,585
  • 1
  • 14
  • 25
0

use std::mutex,

or, you can use std::atomic with std::thread

// std::atomic<UINT8> a;
thread t(f, std::ref(a));
t.detach();
sailfish009
  • 2,561
  • 1
  • 24
  • 31