#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <thread>
#include <vector>
#define NUM_THREADS 8
pthread_mutex_t g_NONSTOP;
std::vector<std::thread> threads;
void demarshal(int thr_id)
{
int epoll_fd;
printf("thr_id:%d\n", thr_id);
std::cout << std::this_thread::get_id() << std::endl;
printf("tid:%lu\n", pthread_self());
while (1) { }
};
int demarshal_start()
{
for(int i = 0; i < NUM_THREADS; ++i)
{
threads.push_back(std::thread(demarshal, i));
sleep(1);
}
return 0;
}
int main()
{
demarshal_start();
// pthread_mutex_init(&g_NONSTOP, NULL);
// pthread_mutex_lock(&g_NONSTOP);
// pthread_mutex_lock(&g_NONSTOP);
for(auto& th : threads) th.join();
return 0;
}
compile
g++ example.cc -lpthread -std=c++11
참조:
https://rafalcieslak.wordpress.com/2014/05/16/c11-stdthreads-managed-by-a-designated-class/