提交 4e784c40 编写于 作者: L libb

add cmake01

Change-Id: Iec6bdf357021da0ea9983dfaa2f6c3b9ce14924c
上级 119cfc01
cmake_minimum_required(VERSION 3.5)
project(cmake01 C CXX)
set(CMAKE_CXX_COMPLICE '/usr/bin/clang++')
#set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(SOURCE_FILES main.cpp Thread.h)
add_executable(cmake01 ${SOURCE_FILES})
target_link_libraries(cmake01)
#ifndef THREAD_H
#define THREAD_H
#include <pthread.h>
#include <functional>
#include <iostream>
#include <assert.h>
#include <string>
class thread_data
{
public:
thread_data(std::function<void()> &f):fun(f){}
std::function<void()> fun;
};
class Thread
{
public:
explicit Thread(std::function<void()> threadFunction, std::string thread_name):thread_function_(threadFunction), name_(thread_name)
{
start_ =false;
join_ = false;
}
~Thread()
{
if(start_ && !join_)
{
pthread_detach(tid_);
}
}
std::string name()
{
return name_;
}
void start()
{
assert(!start_);
start_ = true;
thread_data *thread_data_ptr = new thread_data(thread_function_);
pthread_create(&tid_, NULL, run, thread_data_ptr);
}
void stop()
{
pthread_detach(tid_);
}
int join()
{
assert(!join_);
join_ = true;
return pthread_join(tid_, NULL);
}
Thread(const Thread&) = delete;
Thread& operator=(const Thread&) = delete;
private:
static void* run(void* obj)
{
//±ÿ–Π«æ≤赃≥…‘±∫Ø ˝≤≈ø…“‘±ªµ˜”√
//ƒ«√¥Œ Ã‚¿¥¡À ‘ı√¥∞Ï
thread_data* data = static_cast<thread_data*>(obj);
data->fun();
delete data;
return NULL;
}
std::function<void()> thread_function_;
pthread_t tid_;
bool start_;
bool join_;
std::string name_;
};
#endif
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <string>
#include <string.h>
#include <functional>
#include "Thread.h"
class EventLoopThread
{
public:
EventLoopThread(std::string &name);
~EventLoopThread();
EventLoopThread(const EventLoopThread&) = delete;
EventLoopThread& operator = (const EventLoopThread) = delete;
void startRun();
private:
void threaFun();
// EventLoop *loop_;
bool exiting_;
Thread thread_;
// Mutex mutex_;
// Condition condition_;
bool quit_;
};
//EventLoopThread::EventLoopThread(std::string &name):
//loop_(nullptr), thread_(std::bind(&EventLoopThread::threaFun, this), name),
//mutex_(), condition_(mutex_)
EventLoopThread::EventLoopThread(std::string &name):
thread_(std::bind(&EventLoopThread::threaFun, this), name)
{
}
EventLoopThread::~EventLoopThread()
{
exiting_ = true;
// if(loop_ != nullptr)
// {
// loop_->quit();
// thread_.join();
// }
thread_.join();
}
void EventLoopThread::startRun()
{
thread_.start();
// {
// MutexGuard lock(mutex_);
// while(loop_ == nullptr)
// {
// condition_.wait();
// }
// }
//
// return loop_;
}
void EventLoopThread::threaFun()
{
// EventLoop loop;
// {
// MutexGuard lock(mutex_);
// loop_ = &loop;
// condition_.signal();
// }
//
// loop_->run();
// loop_= nullptr;
quit_ = false;
int i = 0;
while (!quit_) {
std::cout << thread_.name() << "--" << i++ << std::endl;
sleep(1);
}
}
int main(int argc, char **argv)
{
std::string threadName = "thread01";
EventLoopThread *pthread = new EventLoopThread(threadName);
pthread->startRun();
delete pthread;
return 0;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册