Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mickaelblet/thread
One header file std::thread for C++98
https://github.com/mickaelblet/thread
cpp98 cpp98-compatible header-only header-only-library
Last synced: about 2 months ago
JSON representation
One header file std::thread for C++98
- Host: GitHub
- URL: https://github.com/mickaelblet/thread
- Owner: MickaelBlet
- License: mit
- Created: 2023-03-02T02:26:53.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-12T03:18:22.000Z (11 months ago)
- Last Synced: 2024-10-19T19:59:50.175Z (3 months ago)
- Topics: cpp98, cpp98-compatible, header-only, header-only-library
- Language: C++
- Homepage:
- Size: 70.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Thread
std::thread for C++ 98 on one header only file.
[thread.h](include/blet/thread.h)
## QuickStart
``` cpp
#include#include "blet/thread.h"
void functionExampleWithRefArg(double& d) {
d += 1.0;
std::cout << "Inside functionExampleWithRefArg(" << d << ")" << std::endl;
}void functionExampleWithArg(double d) {
std::cout << "Inside functionExampleWithArg(" << d << ")" << std::endl;
}void functionExample(void) {
std::cout << "Inside functionExample" << std::endl;
}int main(int argc, char* argv[]) {
(void)argc;
(void)argv;std::cout << "=== Start ===" << std::endl;
blet::Thread thrd;
thrd.start(&functionExample);
thrd.join();
thrd.start(&functionExampleWithArg, 42.42);
thrd.join();
double d = 42.42;
// type of first argument (double&)
thrd.start(&functionExampleWithRefArg, d);
thrd.join();
std::cout << "New value of d: " << d << std::endl;
std::cout << "=== End ===" << std::endl;
return 0;
}// output:
// === Start ===
// Inside functionExample
// Inside functionExampleWithArg(42.42)
// Inside functionExampleWithRefArg(43.42)
// New value of d: 43.42
// === End ===
```## Member method
``` cpp
#include#include "blet/thread.h"
class Example {
public:
Example() :
_privateVar(0) {}void methodExampleWithRefArg(double& d) {
d += 1.0;
++_privateVar;
std::cout << "Inside methodExampleWithRefArg(" << d << ")" << std::endl;
}void methodExampleWithArg(double d) {
++_privateVar;
std::cout << "Inside methodExampleWithArg(" << d << ")" << std::endl;
}void methodExample(void) {
++_privateVar;
std::cout << "Inside methodExample" << std::endl;
}const int& getPrivateVar() const {
return _privateVar;
}private:
int _privateVar;
};int main(int argc, char* argv[]) {
(void)argc;
(void)argv;std::cout << "=== Start ===" << std::endl;
Example example;
std::cout << "Example private var: " << example.getPrivateVar() << std::endl;
blet::Thread thrd;
thrd.start(&Example::methodExample, &example);
thrd.join();
std::cout << "Example private var: " << example.getPrivateVar() << std::endl;
thrd.start(&Example::methodExampleWithArg, &example, 42.42);
thrd.join();
std::cout << "Example private var: " << example.getPrivateVar() << std::endl;
double d = 42.42;
// type of object, type of first argument (double&)
thrd.start(&Example::methodExampleWithRefArg, &example, d);
thrd.join();
std::cout << "New value of d: " << d << std::endl;
std::cout << "Example private var: " << example.getPrivateVar() << std::endl;
std::cout << "=== End ===" << std::endl;
return 0;
}// output:
// === Start ===
// Example private var: 0
// Inside methodExample
// Example private var: 1
// Inside methodExampleWithArg(42.42)
// Example private var: 2
// Inside methodExampleWithRefArg(43.42)
// New value of d: 43.42
// Example private var: 3
// === End ===
```