Note that there are some explanatory texts on larger screens.

plurals
  1. POPorting a threaded C program to Python
    text
    copied!<p>As an exercise, I would like to port some of the functionality of a threaded C program (using pthreads) to Python.</p> <p>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). </p> <p>The main body of the program reacts to the contents of that data structure in various ways.</p> <p>From what I've read, Python has problems with concurrency.<br> Am I going to be able to implement this?<br> If so, do you have any advice?</p> <p>I've just recently started using Python regularly, and I'm loving it.</p> <p>FYI, the source code resembles this:</p> <pre><code>// 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-&gt;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(&amp;ctx-&gt;my_mutex) thread_control = TH_RUNNABLE; // start the thread pthread_create ( &amp;ctx-&gt;my_thread, NULL, thread_func, ctx ); } int stop_thread() { thread_control = TH_STOPRQ; } int main() { struct context_t *ctx; start_thread(ctx); // do stuff } </code></pre> <p>Thanks!!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload