Created
December 11, 2014 21:00
-
-
Save fiksn/a7dcc1b7d83bacc16c35 to your computer and use it in GitHub Desktop.
libpthread problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
g++ -g -Wall -Werror -pipe -O3 -Wno-deprecated break_pthreads.cpp -o break_pthreads.o -c | |
g++ -g -pthread break_pthreads.o -o break_pthreads | |
./break_pthreads | |
*/ | |
#include <iostream> | |
#include <pthread.h> | |
#include <stdlib.h> | |
#include <vector> | |
#include <signal.h> | |
#include <unistd.h> | |
struct thread_data { | |
int tid; | |
}; | |
struct work { | |
int i; | |
}; | |
void clean_exit_on_sig(int sig_num) | |
{ | |
std::cout << "Got signal " << sig_num << std::endl; | |
exit(1); | |
} | |
unsigned int NUM_THREADS; | |
#define WORKTOGET 5 | |
std::vector<work> toDo; | |
pthread_mutex_t work_lock = PTHREAD_MUTEX_INITIALIZER; | |
void finishWork(work w) | |
{ | |
} | |
void *worker(void *threadarg) | |
{ | |
work workingOn[WORKTOGET]; | |
while (true) | |
{ | |
unsigned int workgot = 0; | |
pthread_mutex_lock(&work_lock); | |
if (toDo.empty()) | |
{ | |
pthread_mutex_unlock(&work_lock); | |
pthread_yield(); | |
} else { | |
while (!toDo.empty() && (workgot < WORKTOGET)) | |
{ | |
workingOn[workgot] = toDo.back(); | |
toDo.pop_back(); | |
workgot++; | |
} | |
} | |
pthread_mutex_unlock(&work_lock); | |
for (unsigned int i = 0; i < workgot; i++) | |
{ | |
finishWork(workingOn[i]); | |
} | |
} | |
return NULL; | |
} | |
int main( int argc, char **argv ) | |
{ | |
bool done=false; | |
/* Set up SIGSEGV handler */ | |
signal(SIGSEGV, clean_exit_on_sig); | |
/* Set up pthreads stuff */ | |
NUM_THREADS = sysconf(_SC_NPROCESSORS_CONF); | |
pthread_t *workers = (pthread_t*)malloc(NUM_THREADS * sizeof(pthread_t)); | |
struct thread_data *children = (struct thread_data*)malloc(NUM_THREADS * sizeof(struct thread_data)); | |
int rc; | |
pthread_attr_t attr; | |
/* Initialize and set thread detached attribute */ | |
pthread_attr_init(&attr); | |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); | |
for (unsigned int t = 0; t < NUM_THREADS; t++) | |
{ | |
children[t].tid = t; | |
rc = pthread_create(&workers[t], &attr, &worker, (void *)&children[t]); | |
if (rc) | |
{ | |
std::cerr << "ERROR: return code from pthread_create() is " << rc << std::endl; | |
exit(-1); | |
} | |
} | |
/* wait for events */ | |
while ( !done ) | |
{ | |
} | |
return( 0 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment