User Tools

Site Tools


pth04.cpp
/*
 * Author, Copyright: Oleg Borodin <onborodin@gmail.com>
 */
 
 
#include <iostream>
#include <chrono>
#include <thread>
 
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
 
 
typedef struct {
    string id;
    string message;
} pth_args_t;
 
void *pth(void *args) {
    auto pth_args = (pth_args_t*)args;
    sleep_for(milliseconds(1000));
    cout << "#pth " << pth_args->id << endl;
    sleep_for(milliseconds(1000));
    cout << "#pth " << pth_args->message << endl;
    pthread_exit(0);
}
 
 
int main(int argc, char **argv) {
 
    pthread_attr_t pth_attr;
    pthread_t pth_id;
 
    pth_args_t pth_args;
    pth_args.id         = "001";
    pth_args.message    = "hi";
 
    pthread_attr_init(&pth_attr);
    pthread_create(&pth_id, &pth_attr, pth, (void *)&pth_args);
    pthread_join(pth_id, NULL);
 
    return 0;
}