Painless
A framework to ease parallelization of sequential CDCL SAT solvers
Loading...
Searching...
No Matches
Threading.hpp
1#pragma once
2
3#include <pthread.h>
4#include <signal.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <time.h>
8
9#define TESTRUN(cmd, msg) \
10 int res = cmd; \
11 if (res != 0) { \
12 printf(msg, res); \
13 exit(res); \
14 }
15
17class Mutex
18{
19 public:
21 Mutex() { TESTRUN(pthread_mutex_init(&mtx, NULL), "Mutex init failed with msg %d") }
22
24 virtual ~Mutex() { TESTRUN(pthread_mutex_destroy(&mtx), "Mutex destroy failed with msg %d") }
25
27 void lock() { TESTRUN(pthread_mutex_lock(&mtx), "Mutex lock failed with msg %d") }
28
30 void unlock() { TESTRUN(pthread_mutex_unlock(&mtx), "Mutex unlock failed with msg %d") }
31
33 bool tryLock()
34 {
35 // return true if lock acquired
36 return pthread_mutex_trylock(&mtx) == 0;
37 }
38
39 protected:
41 pthread_mutex_t mtx;
42};
43
45class Thread
46{
47 public:
49 Thread(void* (*main)(void*), void* arg) { pthread_create(&myTid, NULL, main, arg); }
50
52 void join() { pthread_join(myTid, NULL); }
53
54 void setThreadAffinity(int coreId)
55 {
56 cpu_set_t cpuset;
57 CPU_ZERO(&cpuset);
58 CPU_SET(coreId, &cpuset);
59
60 pthread_setaffinity_np(this->myTid, sizeof(cpu_set_t), &cpuset);
61 }
62
63 protected:
65 pthread_t myTid;
66};
Mutex class.
Definition Threading.hpp:18
pthread_mutex_t mtx
A pthread mutex.
Definition Threading.hpp:41
void unlock()
Unlock the mutex.
Definition Threading.hpp:30
Mutex()
Constructor.
Definition Threading.hpp:21
virtual ~Mutex()
Destructor.
Definition Threading.hpp:24
bool tryLock()
Try to lock the mutex, return true if succes else false.
Definition Threading.hpp:33
void lock()
Lock the mutex.
Definition Threading.hpp:27
Thread class.
Definition Threading.hpp:46
void join()
Join the thread.
Definition Threading.hpp:52
pthread_t myTid
The id of the pthread.
Definition Threading.hpp:65
Thread(void *(*main)(void *), void *arg)
Constructor.
Definition Threading.hpp:49