As an exercise, I would like to port some of the functionality of a threaded C program (using pthreads) to Python.
The C program spawns a thread to (almost) constantly iterate through certain directories, filling a data structure with their contents; these directories change crazy frequently (it's a mail queue).
The main body of the program reacts to the contents of that data structure in various ways.
From what I've read, Python has problems with concurrency.
Am I going to be able to implement this?
If so, do you have any advice?
I've just recently started using Python regularly, and I'm loving it.
FYI, the source code resembles this:
// big struct for everything
struct context_t {
struct datastruct_t data;
pthread_t my_thread;
pthread_mutex_t my_mutex;
}
int thread_control;
// does the dirty work
void *thread_func(void *arg) {
while ( thread_control == TH_RUNNABLE ) {
// loop through dirs
// fill up ctx->data
sleep(1);
}
pthread_mutex_unlock ( my_mutex );
thread_control = TH_STOPPED;
pthread_exit(NULL);
}
int start_thread(struct context_t* ctx) {
// get a mutex to control access to our data
pthread_mutex_trylock(&ctx->my_mutex)
thread_control = TH_RUNNABLE;
// start the thread
pthread_create ( &ctx->my_thread, NULL, thread_func, ctx );
}
int stop_thread() {
thread_control = TH_STOPRQ;
}
int main() {
struct context_t *ctx;
start_thread(ctx);
// do stuff
}
Thanks!!!